各種頁面定時跳轉(倒計時跳轉)代碼總結


一、使用setTimeout函數實現定時跳轉(如下代碼要寫在body區域內)

1
2
3
4
<script type= "text/javascript" >
     //3秒鍾之后跳轉到指定的頁面
  setTimeout(window.location.href= 'http://www.baidu.com' , 3 );
</script>

  或者:

1
2
3
4
5
6
<script language= "JavaScript" type= "text/javascript" >
         function Redirect(){
                     window.location = "add.jsp" ;   //要跳轉的頁面
             }
         setTimeout( 'Redirect()' , 3000 );       //第二個參數是時間,單位毫秒
   </script>

  

 

二、html代碼實現,在頁面的head區域塊內加上如下代碼

1
2
<!-- 5 秒鍾后跳轉到指定的頁面-->
<meta http-equiv= "refresh" content= "5;url=http://www.baidu.com" />

 

三、使用腳本語言(就一句簡單)

1
<%response.setHeader( "Refresh" , "3;url=index.jsp" );%>

 

四、稍微復雜點,多見於登陸之后的定時跳轉(Jsp 頁面的定時的跳轉(數字倒數)

  方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!doctype html>
<head>
<meta charset=utf- 8 " />
<title>js定時跳轉頁面的方法</title>
</head>
<body>
<script>
  var t= 10 ; //設定跳轉的時間
  setInterval( "refer()" , 1000 ); //啟動1秒定時
  function refer(){
     if (t== 0 ){
      location= "http://www.baidu.com" ; //#設定跳轉的鏈接地址
    }
    document.getElementById( 'show' ).innerHTML= "" +t+ "秒后跳轉到百度" ; // 顯示倒計時
    t--; // 計數器遞減
     //本文轉自:
  }
</script>
<span id= "show" ></span>
</body>
</html>

  方法二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<%@ page language= "java"  import = "java.util.*"  contentType= "text/html; charset=UTF-8"
     pageEncoding= "UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
  
<!-- 完成頁面定時的跳轉 -->
<meta http-equiv= "refresh" content= "5;url=/Web_01/main.html" >
  
<title>Insert title here</title>
</head>
<body onload= "run()" >
    頁面將在<span id= "spanId" > 5 </span>秒后跳轉!!
</body>
<br>
<script type= "text/javascript" >
     // 頁面一加載完成,該方法就會執行
     // 讀秒,一秒鍾數字改變一次
     var x = 5 ;
     function run(){
         // 獲取到的是span標簽的對象
         var span = document.getElementById( "spanId" );
         // 獲取span標簽中間的文本
         span.innerHTML = x;
         x--;
         // 再讓run方法執行呢,一秒鍾執行一次
         window.setTimeout( "run()" , 1000 );
     }
</script>
</html>

  方法三:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<html>
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=gb2312" />
<title></title>
<script type= "text/javascript" >    
   function countDown(secs,surl){        
      var jumpTo = document.getElementById( 'jumpTo' );
      jumpTo.innerHTML=secs; 
       if (--secs> 0 ){    
          setTimeout( "countDown(" +secs+ ",'" +surl+ "')" , 1000 );    
        }    
       else {      
          location.href=surl;    
        -ma
        }    
    }    
</script>
</head><br>
<body>
<h1>提交成功</h1> 
<a href= "http://www.so.com" ><span id= "jumpTo" > 3 </span>秒后系統會自動跳轉,也可點擊本處直接跳</a>
<script type= "text/javascript" >
     countDown( 3 , 'http://www.so.com/' );
</script> 
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM