Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

waiting two seconds then auto redirect another page in jquery?

the html structure:

<div class="user_register border_y">
<div>
<span style="color:green;"> after  two seconds then auto redirect </span><br><br> 

<a href="http://example.com/down/test.html">if you don't want to wait you can click this。</a></div></div>

i want to use jquery to get: after two seconds then auto redirect to the a label page. how should i do?

like image 402
zhuanzhou Avatar asked Aug 06 '11 02:08

zhuanzhou


2 Answers

You don't even need jQuery; vanilla JS will work fine...

<a id="link" href="http://example.com">Go NOW!!!</a>

JS:

window.setTimeout(function() {
location.href = document.getElementsByClassName("user_register border_y")[0].getElementsByTagName("a")[0].href;
}, 2000);

getElementsByClassName doesn't work in all browsers; so make sure you provide a fallback

like image 87
Thomas Shields Avatar answered Oct 13 '22 09:10

Thomas Shields


Use setTimeout:

setTimeout(function() { 
    window.location.href = $("a")[0].href; 
 }, 2000);
like image 20
karim79 Avatar answered Oct 13 '22 08:10

karim79