Javascript Cookie 操作包 | Cookie 操作函数
这个美味地“小甜饼”是哪个网站都缺少不了的,“让我们讨一下 Cookie 小怪兽的欢心,做一块儿小甜饼吧~~”:来看源码:
/**
* jscript.storage package
* This package contains functions for doing client-side storage, including
* cookie functions.
*/
if (typeof jscript == 'undefined') {
jscript = function() { }
}
jscript.storage = function() { }
/**
* Sets a cookie.
*(设置 Cookie)
* @param inName The name of the cookie to set.
* @param inValue The value of the cookie.
* @param inExpiry A Date object representing the expiration date of the
* cookie.
*/
jscript.storage.setCookie = function(inName, inValue, inExpiry) {
if (typeof inExpiry == "Date") {
inExpiry = inExpiry.toGMTString();
}
document.cookie = inName + "=" + escape(inValue) + "; expires=" + inExpiry;
} // End setCookie().
/**
* Gets thbe value of a specified cookie. Returns null if cookie isn't found.
*(得到指定 Cookie 的值)
* @param inName The name of the cookie to get the value of.
*/
jscript.storage.getCookie = function(inName) {
var docCookies = document.cookie;
var cIndex = docCookies.indexOf(inName + "=");
if (cIndex == -1) {
return null;
}
cIndex = docCookies.indexOf("=", cIndex) + 1;
var endStr = docCookies.indexOf(";", cIndex);
if (endStr == -1) {
endStr = docCookies.length;
}
return unescape(docCookies.substring(cIndex, endStr));
} // End getCookie().
/**
* Deletes a cookie.(删除 Cookie)
*/
jscript.storage.deleteCookie = function(inName) {
if (this.getCookie(inName)) {
this.setCookie(inName, null, "Thu, 01-Jan-1970 00:00:01 GMT");
}
} // End deleteCookie().
点击试一下:


浙公网安备 33010602011771号