码迷,mamicode.com
首页 > 其他好文 > 详细

使用cookie记录页面跳转次数,然后从最后一级页面跳转回首页面

时间:2017-06-01 16:38:23      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:tco   跳转   设置   list   str   getc   nbsp   date()   页面跳转   

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

使用cookie记录页面跳转次数,然后从最后一级页面跳转回首页面

标签:tco   跳转   设置   list   str   getc   nbsp   date()   页面跳转   

原文地址:http://www.cnblogs.com/limeiky/p/6927496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!