Reputation: 2453
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?
Upvotes: 10
Views: 66032
Reputation: 617
Use this simple code on body OnLoad
<body onLoad="setTimeout(location.href='http://www.newpage.com', '2000')">
Upvotes: 1
Reputation: 193
Use JavaScript to redirect. For example:
<script type="text/javascript">
var time = 15; // Time coutdown
var page = "http://www.redirect-url.com/";
function countDown(){
time--;
gett("timecount").innerHTML = time;
if(time == -1){
window.location = page;
}
}
function gett(id){
if(document.getElementById) return document.getElementById(id);
if(document.all) return document.all.id;
if(document.layers) return document.layers.id;
if(window.opera) return window.opera.id;
}
function init(){
if(gett('timecount')){
setInterval(countDown, 1000);
gett("timecount").innerHTML = time;
}
else{
setTimeout(init, 50);
}
}
document.onload = init();
</SCRIPT>
add this code after body tab
<h3>Auto redirect after <span id="timecount"></span> second(s)!</h3>
Upvotes: 0
Reputation: 8942
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
Upvotes: 18
Reputation: 8767
Below is a simple script based off of your description:
function redirect(){
window.location = $('a').attr('href');
}
setTimeout(redirect, 2000);
Demo: http://jsfiddle.net/KesU9/
Upvotes: 4
Reputation: 322502
Why use JavaScript?
<head>
<meta http-equiv="refresh" content="2;url=http://example.com/down/test.html">
</head>
<body>
<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>
Upvotes: 11
Reputation: 342635
Use setTimeout
:
setTimeout(function() {
window.location.href = $("a")[0].href;
}, 2000);
Upvotes: 16