Html页面跳转的5种方法

2017-09-29 建站知识 浏览 手机预览
文章来源:http://www.imtr.cn/html/n157.html

1、用html实现

<meta http-equiv="refresh" content="10"><!-- 每10秒刷新一次页面 -->
<meta http-equiv="refresh" content="10;url=index.html"><!-- 10秒后跳转到指定链接 -->


2、用javascript实现

<script language="javascript" type="text/javascript">
window.location.href='index.html';// 直接跳转
</script>
<script language="javascript" type="text/javascript">
setTimeout("javascript:location.href='index.html'", 5000);// 5秒后跳转
</script>

优点:灵活,可以结合更多的其他功能

缺点:受到不同浏览器的影响


3、带倒计时的javascript实现方法(IE 浏览器)

<span id="totalSecond">5</span><!-- 5秒后跳转 -->
<script language="javascript" type="text/javascript">
var second = totalSecond.innerText;
setInterval("redirect()", 1000);
function redirect(){
totalSecond.innerText=--second;
if(second<0) location.href='index.html';
}
</script>

优点:更人性化
缺点:firefox不支持(firefox不支持span、div等的innerText属性)


4、结合了倒数的javascript实现(firefox 火狐浏览器)

<script language="javascript" type="text/javascript">
var second = document.getElementById('totalSecond').textContent;
setInterval("redirect()", 1000);
function redirect()
{
document.getElementById('totalSecond').textContent = --second;
if (second < 0) location.href = 'index.html';
}
</script>


5、解决Firefox不支持innerText的问题

<span id="totalSecond">5</span>
<script language="javascript" type="text/javascript">
if(navigator.appName.indexOf("Explorer") > -1){
document.getElementById('totalSecond').innerText = "my text innerText";
} else{
document.getElementById('totalSecond').textContent = "my text textContent";
}
</script>


6、该方法对上面的3和4进行了整合

<span id="totalSecond">5</span>
<script language="javascript" type="text/javascript">
var second = document.getElementById('totalSecond').textContent;
 
if (navigator.appName.indexOf("Explorer") > -1)  {
    second = document.getElementById('totalSecond').innerText;
} else {
    second = document.getElementById('totalSecond').textContent;
} 
setInterval("redirect()", 1000);
function redirect() {
if (second < 0) {
    location.href = 'index.html';
} else {
    if (navigator.appName.indexOf("Explorer") > -1) {
        document.getElementById('totalSecond').innerText = second--;
    } else {
        document.getElementById('totalSecond').textContent = second--;
    }
}
}
</script>
原文地址:http://www.imtr.cn/html/n157.html
  • 如果你的问题还没有解决,可以点击页面右侧的“ ”,站长收到问题后会尽快回复解决方案到你的邮箱。
  • 创造始于问题,有了问题才会思考,有了思考,才有解决问题的方法,才有找到独立思路的可能。 —— 陶行知