- window.CookieUtils = {
cacheCookie : {
cookieValue : undefined,
cookieObj : undefined,
buildObj : function(cookieValue) {
if(cookieValue) {
this.cookieValue = cookieValue;
this.cookieObj = {};
var arr = cookieValue.split(";");
for(var i = 0; i < arr.length; i++){
var kvs = arr[i].split("=");
if(kvs.length === 2) {
this.cookieObj[kvs[0].trim()] = kvs[1].trim();
}
}
}
}
},
//获取cookie中的值
getCookie : function(name) {
if(document.cookie != this.cacheCookie.cookieValue) {
this.cacheCookie.buildObj(document.cookie);
}
if(this.cacheCookie.cookieObj && this.cacheCookie.cookieObj[name]) {
var value = this.cacheCookie.cookieObj[name];
return unescape(value);
}
return '';
},
//设置cookie
setCookie : function(name, value, days, path){
var expires=-1;
if (days && typeof days == "number" && days >= 0) {
var d = new Date();
d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
expires = d.toGMTString();
}
value=escape(value);
document.cookie = name + "=" + value + ";"
+ (expires != -1 ? " expires=" + expires + ";" : "")
+ (path ? "path=" + path : "");
this.cacheCookie.buildObj(document.cookie);
},
//删除cookie
delCookie : function(name){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = this.getCookie(name);
if(cval!=null) {
document.cookie = name + "=" + cval + ";expires="
+ exp.toGMTString();
this.cacheCookie.buildObj(document.cookie);
}
}
}