(1)使用setTimeout函數實現定時跳轉(如下代碼要寫在body區域內)
1 <script type="text/javascript"> 2 //3秒鍾之后跳轉到指定的頁面 3 setTimeout(window.location.href='http://www.baidu.com',3); 4 </script>
(2)html代碼實現,在頁面的head區域塊內加上如下代碼
1 <!--5秒鍾后跳轉到指定的頁面--> 2 <meta http-equiv="refresh" content="5;url=http://www.baidu.com" />
(3)稍微復雜點,多見於登陸之后的定時跳轉
1 <!doctype html> 2 <head> 3 <meta charset=utf-8" /> 4 <title>js定時跳轉頁面的方法</title> 5 </head> 6 <body> 7 <script> 8 var t=10;//設定跳轉的時間 9 setInterval("refer()",1000); //啟動1秒定時 10 function refer(){ 11 if(t==0){ 12 location="http://www.baidu.com"; //#設定跳轉的鏈接地址 13 } 14 document.getElementById('show').innerHTML=""+t+"秒后跳轉到百度"; // 顯示倒計時 15 t--; // 計數器遞減 16 //本文轉自: 17 } 18 </script> 19 <span id="show"></span> 20 </body> 21 </html>