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

    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

    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

    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. }  
    1. 另一种方法删除cookie  
    1. setcookie("cookiename","")设置某个cookiename值为空  

    4.跳转代码

    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.         }  
  • 相关阅读:
    Netty实现原理浅析
    Netty
    JAVA调用Rest服务接口
    泛型约束
    RegisterStartupScript和RegisterClientScriptBlock的用法
    TFS 2010 使用手册(四)备份与恢复
    TFS 2010 使用手册(三)权限管理
    TFS 2010 使用手册(二)项目集合与项目
    TFS 2010 使用手册(一)安装与配置
    错误"Lc.exe 已退出,代码 -1 "
  • 原文地址:https://www.cnblogs.com/limeiky/p/6924769.html
Copyright © 2020-2023  润新知