jelly46
jelly46

Reputation: 243

javascript code making url link undefined

I am working on this javascript code and when is goes in the head and I refresh it goes to a 404 page.

Please can someone help.

<script type="text/javascript">
function recordOutboundLink(link, category, action) {
    try {
        var myTracker = _gat._getTrackerByName();
        _gaq.push(['myTracker._trackEvent', category, action]);
        setTimeout('document.location = "' + link.href + '"', 100)
    } catch (err) { }
}
$(document).ready(function () { $('#myid').click(recordOutboundLink(this, 'regular   xxxxx', 'xxxx.example.com')); });
 </script>

Upvotes: 1

Views: 1606

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173532

You're trying to register the result of recordOutboundLink() as a click handler, causing the function to run first, evaluating window.href as the page to redirect to. The value of window.href is typically undefined, so the browser will try to redirect to http://undefined or something similar.

Instead, you should only execute the function when something is clicked, like so:

$(document).ready(function () { 
    $('#myid').click(function() {
        recordOutboundLink(this, 'regular   xxxxx', 'http://xxxx.example.com');
        return false;
    });

I believe the Google docs mention something like this:

<a href="bla bla" onclick="recordOutboundLink(this, 'regular crap', 'http://www.example.com'); return false;">tada click me</a>

Edit

Your locations should always be absolute, i.e. start with http://, https:// or simply //.

Upvotes: 3

bPratik
bPratik

Reputation: 7019

You need to pass the complete url to the method, ie, with the http:// part

so either use:

.click(recordOutboundLink(this, 'regular   xxxxx', 'http://xxxx.example.com'))

or

.click(recordOutboundLink(this, 'regular   xxxxx', '//xxxx.example.com'))

Upvotes: 1

Related Questions