views:

83

answers:

2

I want make a hyperlink turn active and go to that page after a certain amount of time or on pageload. Is this possible with jQuery?

+3  A: 

Try this:

setTimeout(followLink, 10000); // 10 seconds

function followLink() {
    window.location = jQuery('#myLink').attr('href');
}

jQuery(function() {
    followLink();
});

I'll also just note that there's nothing particular to jQuery about this: you could pretty easily do the same thing with plain vanilla JS.

nickf
Yes, the trick in the 'delay' before navigate can be achieved without jquery. Though you can use jquery to easily attach events during runtime. Which one of your examples did.
o.k.w
Calling .click() on a link doesn't trigger its default behavior, though. It has to be done with window.location, AFAICS.
Reinis I.
Why are you calling it on a timeout and on document ready? Makes no sense.
Josh Stodola
From the OP: "go to that page after a certain amount of time or on pageload"
nickf
A: 

I like this way of doing it:


On document ready:

$(function(){
       window.location = $('#link').attr('href');
});


2 seconds after document ready:

$(function(){
    setTimeout(function(){
        window.location = $('#link').attr('href');
    },2000);
});
mofle