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页,即首页面
        }

  • 相关阅读:
    04.Spark的核心组件
    02.Spark 标签生成(Java和Scala的两种实现)
    01.Spark(spark shell实现word count)
    Scala--尾递归--泛型--类型上下限界定--多重界定--视图界定--型变(协变--逆变)--隐式转换--SAM
    01.安装JDK配置环境变量
    Scala模式匹配--样例类--密封样例类--偏函数
    00.Spark--安装Spark
    Scala知识点总结(上半部分)
    01. Java的经典排序--选择排序--冒泡排序--折半查找(二分查找)
    2019-2020学年 20191327《信息安全专业导论》第五周学习总结
  • 原文地址:https://www.cnblogs.com/limeiky/p/6927496.html
Copyright © 2011-2022 走看看