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

  • 相关阅读:
    es手动创建索引,修改索引,删除索引
    es的scoll滚动查询技术
    es倒排索引和正排索引
    linux中按照指定内容查找文件
    es定制排序搜索结果
    es中如何定位不合法搜索
    es中filter和query的对比
    es组合多个条件进行查询
    手动建立mapping以及增加属性
    30分钟学会如何使用Shiro
  • 原文地址:https://www.cnblogs.com/limeiky/p/6927496.html
Copyright © 2011-2022 走看看