通过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