JS 简单封装的Cookie对象

//cookie
var CookieHelper = {
setCookie:function(name, value, expiry, path, domain, secure){
var nameString = "ck_"+name + "=" + value;
var expiryString = "";
if (expiry != null) {
try {
expiryString = "; expires=" + expiry.toGMTString();
catch (e) {
if (expiry) {
var lsd = new Date();
lsd.setTime(lsd.getTime() + expiry * 1000);
expiryString = "; expires=" + lsd.toGMTString();
}
}
}else {
expiryString = "; expires=" + (365*24*60*1000).toGMTString();
}
var pathString = (path == null) ? " ;path=/" : " ;path = " + path;
var domainString = (domain == null) ? " ;domain = " + gloDomain : " ;domain = " + domain;
var secureString = (secure) ? ";secure=" : "";
document.cookie = nameString + expiryString + pathString + domainString + secureString;
},
getCookie : function(name) {
var i, aname, value, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
aname = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
value = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
aname = aname.replace(/^\s+|\s+$/g, "");
if (aname == "ck_"+name) {
return (value);
}
}
return '';
}
};
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

JS&nbsp;简单封装的Cookie对象

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭