码迷,mamicode.com
首页 > Web开发 > 详细

intern的项目纠结js里cookie的用法

时间:2015-08-11 06:58:44      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

cookie有这么几个属性,name,value,expireDate,domain,path

项目遇到html/person/person.html设置的cookie无法从html/alipay/pay.asp中读出

结果:cookie的路径问题,person.html默认的path是自己的路径,想要让隔壁路径的网页访问该cookie须设置path=/

之后,一并把domain也弄清楚了,www.test.com/test/test.aspx默认domain为www.test.com,ti.test.com设置的cookie要让t2.test.com访问须设置domain=.test.com

js原生封装函数cookie:

 1 var CookieUtil={
 2     get:function(name){
 3         var cookieName=encodeURIComponent(name)+"=",
 4         cookieStart=document.cookie.indexOf(cookieName),
 5         cookieValue=null;
 6         if(cookieStart>-1){
 7             var cookieEnd=document.cookie.indexOf(";",cookieStart);
 8             if(cookieEnd==-1){
 9                 cookieEnd=document.cookie.length;
10             }
11             cookieValue=decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length,cookieEnd));
12         }
13         return cookieValue;
14     },
15     set:function(name,value,expires,path,domain,secure){
16         car cookieText=encodeURIComponent(name)+"="+
17         encodeURIComponent(value);
18         if(expires instanceof Date){
19             cookieText+=";expires="+expires.toGMTString();
20         }
21         if(path){
22             cookieText+=";path="+path;
23         }
24         if(domain){
25             cookieText+=";domain="+domain;
26         }
27         if(secure){
28             cookieText+=";secure";
29         }
30         document.cookie=cookieText;
31     },
32     unset:function(name,path,domain,secure){
33         this.set(name,"",new Date(0),path,domain,secure);
34     }
35 };

 

intern的项目纠结js里cookie的用法

标签:

原文地址:http://www.cnblogs.com/xiaoshan-coding/p/4719713.html

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