关闭

JS 简单封装的Cookie对象

97人阅读 评论(0) 收藏 举报

//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 '';
}
};
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:25074次
    • 积分:607
    • 等级:
    • 排名:千里之外
    • 原创:76篇
    • 转载:8篇
    • 译文:0篇
    • 评论:0条
    文章分类