3

I'm trying to delay outgoing links when clicked, so that googles event tracking have time to occur.

I wrote the following code, but I'm unsure of how to pass my variable to window.location. It just adds it as string "url" and not the link adress. What am I doing wrong?

$("a.private-product-order-button").click(function(e) {
   e.preventDefault();
   _gaq.push(['_trackEvent', 'Order buttons', 'Click', 'Service']);
   var url = $(this).attr("href");
   setTimeout(function() {
      $(window.location).attr('href', 'url');
      }, 200);
});
2
  • window.location.href=url - you are over jQuerying it Commented Feb 13, 2013 at 9:37
  • @mplungjan: Is such a thing even possible? Commented Feb 13, 2013 at 9:38

3 Answers 3

2

No need to use jQuery to set a property of the location object (and no need to use jQuery to get href property of the anchor object):

$("a.private-product-order-button").click(function(e) {
    e.preventDefault();
    _gaq.push(['_trackEvent', 'Order buttons', 'Click', 'Service']);

    var url = this.href;
    setTimeout(function() {
        window.location.href = url;
    }, 200);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank's. I'm so new to this, doing things the "right way" is a work in progress.
1

Because you're adding string after all.

It should be:

$(window.location).attr('href', url);

not

$(window.location).attr('href', 'url');

1 Comment

Ohh damnit it was so easy. Thanks.
1

Use $(window.location).attr('href', url); without the quotes around url.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.