使用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" οnclick="goTo(${planId});" value="跳转子页面" />

  <input type="button" class="button" οnclick="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" οnclick="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页,即首页面
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值