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?
views:
83answers:
2
+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
2009-10-12 13:15:38
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
2009-10-12 13:18:18
Calling .click() on a link doesn't trigger its default behavior, though. It has to be done with window.location, AFAICS.
Reinis I.
2009-10-12 13:22:05
Why are you calling it on a timeout and on document ready? Makes no sense.
Josh Stodola
2009-10-12 14:03:56
From the OP: "go to that page after a certain amount of time or on pageload"
nickf
2009-10-12 22:40:09
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
2009-10-12 17:16:55