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) {
var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value); if (expires instanceof Date) {
cookieText += "; expires=" + expires.toDateString(); //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);
}
};

子cookie

//name=name1=value&name2=value&name3=value&name4=value
//子cookie 的处理方法
var SubCookieUtil = {
get: function (name, subName) {
var subCookies = this.getAll(name);
return subCookies ? subCookies[subName]: null;
}, getAll: function (name) {
var cookieName = encodeURIComponent(name) + "=",
cookieStart = document.cookie.indexOf(cookieName),
cookieValue = null,
result = {}; if (cookieStart > -1) {
var cookieEnd = document.cookie.indexOf(';', cookieStart);
if (cookieEnd == -1) {
cookieEnd = document.cookie.length;
} cookieValue = document.cookie.substring(cookieStart + cookieName.length, cookieEnd); if (cookieValue.length > 0) {
var subCookies = cookieValue.split(' & '); for (var i =0, len = subCookies.length; i < len; i++) {
var parts = subCookies[i].split('=');
result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
}
return result;
} return null;
}
}, set: function (name, subName, value, expires, path, domain, secure) {
var subcookies = this.getAll(name) || {};
subcookies[subName] = value;
this.setAll(name, subcookies, expires, path, domain, secure);
},
setAll: function (name, subcookies, expires, path, domain, secure) {
var cookieText = encodeURIComponent(name) + '=';
var subCookieParts = new Array(); for (var subName in subcookies) {
if (subName.length > 0 && subcookies.hasOwnProperty(subName)) {
subCookieParts.push(encodeURIComponent(subName) + '=' + encodeURIComponent(subcookies[subName]));
}
} if (subCookieParts.length > 0) {
cookieText += subCookieParts.join(" & "); if (expires instanceof Date) {
cookieText += '; expires=' + expires.toDateString();
} if (path) {
cookieText += '; path=' + path;
} if (domain) {
cookieText += '; domain' + domain;
} if (secure) {
cookieText += '; secure';
}
} else {
cookieText += '; expires=' + (new Date(0).toDateString());
} document.cookie = cookieText;
}, unset: function (name, subName, path, domain, secure) {
var subcookies = this.getAll(name);
if (subcookies) {
delete subcookies[subName];
this.setAll(name, subcookies, null, path, domain, secure);
}
},
unsetAll: function (name, path, domain, secure) {
this.setAll(name, null, new Date(0), path, domain, secure);
}
};

来自:《JavaScript 高级程序设计第二版》

上一篇:【读书笔记】《算法竞赛进阶指南》读书笔记——0x10基本数据结构


下一篇:VPS搭建VPN、BLOG