通過cookie記錄,設置頁面訪問的跳轉頁


通過cookie記錄,設置頁面訪問的跳轉頁

目的:

1.訪問fm.html時,假如沒訪問過,跳轉到fm1.html,訪問1次后,跳轉到fm2.html,訪問2次后,跳轉到fm3.html,再次訪問跳fm1.html,依次循環

原理:
通過cookie保存訪問記錄:沒訪問過時,保存cookie1,訪問1次后,保存cookie2,訪問2次后,保存cookie3,再次訪問fm.html,清除cookie

關鍵代碼:

1.保存cookie

[html] view plain copy
  1. function setCookie(name,value) {  
  2.             var Days = 365;  
  3.             var exp = new Date();  
  4.             exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);  
  5.             document.cookie = name + ("=" + (value) + ";expires=" + exp.toGMTString() + ";path=/;");}  

2.讀取cookie

[html] view plain copy
  1. function getCookie(name){  
  2.             var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));  
  3.             if (arr != null) {  
  4.                 return (arr[2]);  
  5.             }  
  6.             return null;  
  7. }  


3.刪除cookie

[html] view plain copy
  1. function delCookie(name){//為了刪除指定名稱的cookie,可以將其過期時間設定為一個過去的時間  
  2.    var date = new Date();  
  3.    date.setTime(date.getTime() - 10000);  
  4.    document.cookie = name + "=a; expires=" + date.toGMTString();  
  5. }  
[html] view plain copy
  1. 另一種方法刪除cookie  
[html] view plain copy
  1. setcookie("cookiename","")設置某個cookiename值為空  

4.跳轉代碼

[html] view plain copy
  1. var url=window.location.href;  
  2. var history=getCookie("his");  
  3. if(history==""||history==null){  
  4.     setCookie("his","http://127.0.0.1/fm1.html");  
  5.     window.location.href="fm1.html"  
  6.     }else{  
  7.         var index=history.lastIndexOf("/");  
  8.         var cururl=history.substring(index+3,index+4);  
  9.         var cururll=parseInt(cururl);  
  10.         switch(cururll){  
  11.             case 1:  
  12.                  setCookie("his","http://127.0.0.1/fm2.html");  
  13.                  window.location.href="fm2.html";  
  14.                  break;  
  15.                    
  16.            case 2:  
  17.                   setCookie("his","http://127.0.0.1/fm3.html");  
  18.                   window.location.href="fm3.html";  
  19.                   break;  
  20.             default:  
  21.                  delCookie("his");    
  22.                  window.location.href="fm.html"  
  23.             }  
  24.           
  25.         }  

 


免責聲明!

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



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