您的位置:首页 > Web前端 > JavaScript

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

2015-08-10 23:59 721 查看
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:

var CookieUtil={
get:function(name){
var cookieName=encodeURIComponent(name)+"=",
cookieStart=document.cookie.indexOf(cookieName),
cookieValue=null;
if(cookieStart>-1){
var cookieEnd=document.cookie.indexOf(";",cookieStart);
if(cookieEnd==-1){
cookieEnd=document.cookie.length;
}
cookieValue=decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length,cookieEnd));
}
return cookieValue;
},
set:function(name,value,expires,path,domain,secure){
car cookieText=encodeURIComponent(name)+"="+
encodeURIComponent(value);
if(expires instanceof Date){
cookieText+=";expires="+expires.toGMTString();
}
if(path){
cookieText+=";path="+path;
}
if(domain){
cookieText+=";domain="+domain;
}
if(secure){
cookieText+=";secure";
}
document.cookie=cookieText;
},
unset:function(name,path,domain,secure){
this.set(name,"",new Date(0),path,domain,secure);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: