Trouble clicking dynamic a tag on timeout

196 views Asked by At

I have the following problem which seems easy but I can't figure out what is wrong.

What I want to do is the following:

When I click a link (example: mysite.com/blog/blog-article.html) the href of that link is saved to a variable. In javascript it is blocked and directed to a different url (the main category – mysite.com/blog ). All pages are loaded dynamical.

In this mysite.com/blog there is an a element without a href. I will give the blocked url (that was saved in the variable) to the a element.

$('#go-to-next').attr('href',goToUrl);

This works fine, but after a timeout I want it to be clicked.

setTimeout(function(){
        $('#go-to-next').click();
    },500);

But this isn't working. Anybody knows what the problem could be? When I place the timeout script in firebug it works though. So it has to do something with .on(), but stramge enough I can change the href without any problem.

1

There are 1 answers

2
Barmar On

Try:

setTimeout(function(){
        window.location.href = $('#go-to-next').attr('href');
    },500);

Calling .click() on an anchor doesn't run the default action of following the link, so you have to code that explicitly if it's what you want.