zoukankan      html  css  js  c++  java
  • 使用cookie记录页面跳转次数,然后从最后一级页面跳转回首页面

    1、首先,给出cookie设置,获取,删除的操作函数。

     function setCookie(name,value) {  
            var Days = 30;  
            var exp = new Date();  
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);  
            document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
        }
        function getCookie(name){  
            var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));  
            if (arr != null) {  
                return unescape(arr[2]);  
            }  
            return null;
        }
        function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间  
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval=getCookie(name);
            if(cval!=null)
                document.cookie= name + "="+cval+";expires="+exp.toGMTString();
        }

    2、在列表首页初始化函数中,设置cookie初始值。

     $(document).ready(function(){
            setCookie("his",1);
            //var his=getCookie("his");  //值为1
        });

    3、子页面中的函数。

    HTML中:

      <input type="button" class="button" onclick="goTo(${planId});" value="跳转子页面" />

      <input type="button" class="button" onclick="return(${planId});" value="返回首页" />

    js中:

     function setCookie(name,value) {  
            var Days = 30;  
            var exp = new Date();  
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);  
            document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
        }

     function getCookie(name){  
            var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));  
            if (arr != null) {  
                return unescape(arr[2]);  
            }  
            return null;
        }

     function goTo(id){//子页面继续跳转到二级子页面
            document.location.href="device!gotoupdate.action?projectId="+${projectId};
            //var his=getCookie("his"); //获取初始化时的值为1
            setCookie("his",parseInt(his)+1); //页面刷新跳转时,cookie值加1
           // var his1=getCookie("his"); //此时cookie值为2
        }

     function return(id){//子页面返回首页面
            var his=getCookie("his");//获取cookie的初始化值为1
            history.go(-(parseInt(his)));//向后返回1页,即首页面

        }

    4、在跳转的二级子页面中,获取cookie值,用history.go(-num)返回首页。

    HTML中:

    <input type="button" class="button" onclick="goToList();" value="返回列表" />

    JS中:
        function getCookie(name){  
            var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));  
            if (arr != null) {  
                return unescape(arr[2]);  
            }  
            return null;
        }
        function goToList(){
            var his=getCookie("his");//此时cookie值为2
            history.go(-(parseInt(his)));//向后返回2页,即首页面
        }

  • 相关阅读:
    Your branch and 'origin/master' have diverged, and have # and # different commits each, respectively
    testng dataprovider 的几种用法以及Java中的二维数组
    python 类属性 实例属性 类方法 实例方法 静态方法(转载)
    Webdriver中PageFactory的正确用法
    Selenium webdriver在最开始打开的时候浏览器的地址栏会出现data的解决方法
    Selenium webdriver如何处理confirm对话框的问题
    SoapUI 引用第三方jar包和引用Groovy脚本
    git rebase -i 合并commit
    Git 撤销commit的注意事项
    单进程执行
  • 原文地址:https://www.cnblogs.com/limeiky/p/6927496.html
Copyright © 2011-2022 走看看