Javascript 倒计时和重定向 [英] Javascript countdown and redirect
问题描述
我有一个运行倒计时然后重定向到不同链接的 javascript 脚本.
I have a javascript script that runs a countdown and then redirects to a different link.
由于我现在所有网站都使用静态域运行相同的代码,我想知道是否可以编辑脚本以使其重定向到不同的链接,具体取决于它的span"标签缠绕.
Since I now have all my sites running off the same code using a static domain, I would like to know if it is possible to edit the script to make it redirect to a different link depending on the "span" tag it is wrapped around.
(我也删除了这段代码,所以如果有人有更整洁的方法并想要清理它,那就太好了.)
(I also butchered this code, so if anyone has a tidier way of doing it and wants to clean it up, that'd be great.)
Javascript
var time_left = 3;
var cinterval;
function time_dec(){
time_left--;
document.getElementById('countdown').innerHTML = time_left;
if(time_left == 1){
var originalstring = document.getElementById('countdown2').innerHTML;
var newstring = originalstring.replace('seconds','second');
document.getElementById('countdown2').innerHTML = newstring;
window.location.replace("http://mydomainhere.com");
clearInterval(cinterval);
}
}
cinterval = setInterval('time_dec()', 1000);
HTML
<p>Redirecting you <span id="countdown2">in <span id="countdown">3</span> seconds</span>.</p>
如果有人能帮助我,我将不胜感激.
If anyone could help me out that would be much appreciated.
推荐答案
window.location.replace("http://mydomainhere.com");
这真的是唯一重要的一行.我不使用 span
标签,而是在页面上粘贴一个隐藏元素.
This is really the only important line. Instead of using a span
tag, I'd just stick a hidden element on the page.
<input id="url" type="hidden" value="http://www.adomain.com"/>
然后你获取价值:
var url = document.getElementById("url").getAttribute("value");
window.location.replace(url);
这篇关于Javascript 倒计时和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!