天天看點

HTML頁面定時跳轉方法

1)html的實作

<head>

<meta http-equiv="refresh" content="5;url=hello.html">

</head>

優點:簡單

缺點:Struts Tiles中無法使用

2)javascript的實作

<script language="javascript" type="text/javascript">

setTimeout("javascript:location.href='hello.html'", 5000);

</script>

優點:靈活,可以結合更多的其他功能

缺點:受到不同浏覽器的影響

3)結合了倒數的javascript實作(IE)

<span id="totalSecond">5</span>

var second = totalSecond.innerText;

setInterval("redirect()", 1000);

function redirect(){

totalSecond.innerText=--second;

if(second<0) location.href='hello.html';

}

優點:更人性化

缺點:firefox不支援(firefox不支援span、div等的innerText屬性)

3')結合了倒數的javascript實作(firefox)

var second = document.getElementById('totalSecond').textContent;

function redirect()

{

document.getElementById('totalSecond').textContent = --second;

if (second < 0) location.href = 'hello.html';

4)解決Firefox不支援innerText的問題

if(navigator.appName.indexOf("Explorer") > -1){

document.getElementById('totalSecond').innerText = "my text innerText";

} else{

document.getElementById('totalSecond').textContent = "my text textContent";

5)整合3)和3')

if (navigator.appName.indexOf("Explorer") > -1)

second = document.getElementById('totalSecond').innerText;

} else

second = document.getElementById('totalSecond').textContent;

if (second < 0)

location.href = 'hello.html';

document.getElementById('totalSecond').innerText = second--;

document.getElementById('totalSecond').textContent = second--;

我實驗了  “5)整合3)和3')” 如下:

開始做,堅持做,重複做

下一篇: TOMCAT