HTML页面自动跳转的五种实现方法

来源:互联网 发布:杭州牛盾网络 编辑:程序博客网 时间:2024/05/24 07:29

下面列了五个例子来详细说明,这几个例子的主要功能是:在5秒后,自动跳转到同目录下的hello.html(根据自己需要自行修改)文件。

1)html的实现

view plaincopy to clipboardprint?
  1. <head>  
  2. <meta http-equiv="refresh" content="5;url=hello.html">  
  3. </head>  

优点:简单

缺点:Struts Tiles中无法使用

2)javascript的实现

view plaincopy to clipboardprint?
  1. <mce:script language="javascript" type="text/javascript"><!--  
  2. setTimeout("javascript:location.href='http://liting6680.blog.163.com/blog/hello.html'", 5000);  
  3. // --></mce:script>  

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

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

3)结合了倒数的javascript实现(IE)

view plaincopy to clipboardprint?
  1. <span id="totalSecond">5</span>  
  2. <mce:script language="javascript" type="text/javascript"><!--  
  3. var second = totalSecond.innerText;  
  4. setInterval("redirect()", 1000);  
  5. function redirect(){  
  6. totalSecond.innerText=--second;  
  7. if(second<0) location.href='http://liting6680.blog.163.com/blog/hello.html';  
  8. }  
  9. // --></mce:script>  

优点:更人性化

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

3')结合了倒数的javascript实现(firefox)

view plaincopy to clipboardprint?
  1. <mce:script language="javascript" type="text/javascript"><!--  
  2. var second = document.getElementById('totalSecond').textContent;  
  3. setInterval("redirect()", 1000);  
  4. function redirect()  
  5. {  
  6. document.getElementById('totalSecond').textContent = --second;  
  7. if (second < 0) location.href='http://liting6680.blog.163.com/blog/hello.html';  
  8. }  
  9. // --></mce:script>  

4)解决Firefox不支持innerText的问题

view plaincopy to clipboardprint?
  1. <span id="totalSecond">5</span>  
  2. <mce:script language="javascript" type="text/javascript"><!--  
  3. if(navigator.appName.indexOf("Explorer") > -1){  
  4. document.getElementById('totalSecond').innerText = "my text innerText";  
  5. else{  
  6. document.getElementById('totalSecond').textContent = "my text textContent";  
  7. }  
  8. // --></mce:script>  

5)整合3)和3')

view plaincopy to clipboardprint?
  1. <span id="totalSecond">5</span>  
  2. <mce:script language="javascript" type="text/javascript"><!--  
  3. var second = document.getElementById('totalSecond').textContent;  
  4. if (navigator.appName.indexOf("Explorer") > -1)  
  5. {  
  6. second = document.getElementById('totalSecond').innerText;  
  7. else  
  8. {  
  9. second = document.getElementById('totalSecond').textContent;  
  10. }  
  11. setInterval("redirect()", 1000);  
  12. function redirect()  
  13. {  
  14. if (second < 0)  
  15. {  
  16. location.href='http://liting6680.blog.163.com/blog/hello.html';  
  17. else  
  18. {  
  19. if (navigator.appName.indexOf("Explorer") > -1)  
  20. {  
  21. document.getElementById('totalSecond').innerText = second--;  
  22. else  
  23. {  
  24. document.getElementById('totalSecond').textContent = second--;  
  25. }  
  26. }  
  27. }  
  28. // --></mce:script>