|
|
|
|
@ -3,60 +3,65 @@
|
|
|
|
|
* https://github.com/carhartl/jquery-cookie
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2013 Klaus Hartl
|
|
|
|
|
* Released under the MIT license
|
|
|
|
|
* 根据MIT许可证发布
|
|
|
|
|
*/
|
|
|
|
|
(function (factory) {
|
|
|
|
|
if (typeof define === 'function' && define.amd) {
|
|
|
|
|
// AMD. Register as anonymous module.
|
|
|
|
|
// 如果环境支持AMD规范,则将插件注册为匿名模块。
|
|
|
|
|
define(['jquery'], factory);
|
|
|
|
|
} else {
|
|
|
|
|
// Browser globals.
|
|
|
|
|
// 否则,将插件暴露给全局的jQuery对象。
|
|
|
|
|
factory(jQuery);
|
|
|
|
|
}
|
|
|
|
|
}(function ($) {
|
|
|
|
|
|
|
|
|
|
// 定义一个正则表达式,用于匹配加号(+)。
|
|
|
|
|
var pluses = /\+/g;
|
|
|
|
|
|
|
|
|
|
// 编码cookie值的函数,如果config.raw为true,则返回原始字符串;否则,使用encodeURIComponent进行编码。
|
|
|
|
|
function encode(s) {
|
|
|
|
|
return config.raw ? s : encodeURIComponent(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解码cookie值的函数,如果config.raw为true,则返回原始字符串;否则,使用decodeURIComponent进行解码。
|
|
|
|
|
function decode(s) {
|
|
|
|
|
return config.raw ? s : decodeURIComponent(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将值转换为字符串形式,以便存储在cookie中的函数。如果config.json为true,则使用JSON.stringify将对象转换为JSON字符串。
|
|
|
|
|
function stringifyCookieValue(value) {
|
|
|
|
|
return encode(config.json ? JSON.stringify(value) : String(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析cookie值的函数。首先处理引号和反斜杠,然后尝试解码和解析JSON。
|
|
|
|
|
function parseCookieValue(s) {
|
|
|
|
|
if (s.indexOf('"') === 0) {
|
|
|
|
|
// This is a quoted cookie as according to RFC2068, unescape...
|
|
|
|
|
// 根据RFC2068,如果是带引号的cookie,进行反转义...
|
|
|
|
|
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Replace server-side written pluses with spaces.
|
|
|
|
|
// If we can't decode the cookie, ignore it, it's unusable.
|
|
|
|
|
// 将服务器端写入的加号替换为空格。
|
|
|
|
|
// 如果我们不能解码cookie,忽略它,它是不可用的。
|
|
|
|
|
s = decodeURIComponent(s.replace(pluses, ' '));
|
|
|
|
|
} catch(e) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// If we can't parse the cookie, ignore it, it's unusable.
|
|
|
|
|
// 如果我们不能解析cookie,忽略它,它是不可用的。
|
|
|
|
|
return config.json ? JSON.parse(s) : s;
|
|
|
|
|
} catch(e) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 读取cookie值,并根据提供的转换函数(如果存在)进行转换的函数。
|
|
|
|
|
function read(s, converter) {
|
|
|
|
|
var value = config.raw ? s : parseCookieValue(s);
|
|
|
|
|
return $.isFunction(converter) ? converter(value) : value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 定义$.cookie函数,它是插件的核心,用于读写cookie。
|
|
|
|
|
var config = $.cookie = function (key, value, options) {
|
|
|
|
|
|
|
|
|
|
// Write
|
|
|
|
|
// 写入cookie的逻辑。
|
|
|
|
|
if (value !== undefined && !$.isFunction(value)) {
|
|
|
|
|
options = $.extend({}, config.defaults, options);
|
|
|
|
|
|
|
|
|
|
@ -67,20 +72,18 @@
|
|
|
|
|
|
|
|
|
|
return (document.cookie = [
|
|
|
|
|
encode(key), '=', stringifyCookieValue(value),
|
|
|
|
|
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
|
|
|
|
|
options.expires ? '; expires=' + options.expires.toUTCString() : '', // 使用expires属性,max-age不受IE支持
|
|
|
|
|
options.path ? '; path=' + options.path : '',
|
|
|
|
|
options.domain ? '; domain=' + options.domain : '',
|
|
|
|
|
options.secure ? '; secure' : ''
|
|
|
|
|
].join(''));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read
|
|
|
|
|
|
|
|
|
|
// 读取cookie的逻辑。
|
|
|
|
|
var result = key ? undefined : {};
|
|
|
|
|
|
|
|
|
|
// To prevent the for loop in the first place assign an empty array
|
|
|
|
|
// in case there are no cookies at all. Also prevents odd result when
|
|
|
|
|
// calling $.cookie().
|
|
|
|
|
// 为了防止在没有cookie的情况下进行for循环,预先分配一个空数组。
|
|
|
|
|
// 也防止在调用$.cookie()时得到奇怪的结果。
|
|
|
|
|
var cookies = document.cookie ? document.cookie.split('; ') : [];
|
|
|
|
|
|
|
|
|
|
for (var i = 0, l = cookies.length; i < l; i++) {
|
|
|
|
|
@ -89,12 +92,12 @@
|
|
|
|
|
var cookie = parts.join('=');
|
|
|
|
|
|
|
|
|
|
if (key && key === name) {
|
|
|
|
|
// If second argument (value) is a function it's a converter...
|
|
|
|
|
// 如果第二个参数(value)是一个函数,它就是一个转换器...
|
|
|
|
|
result = read(cookie, value);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prevent storing a cookie that we couldn't decode.
|
|
|
|
|
// 阻止存储我们无法解码的cookie。
|
|
|
|
|
if (!key && (cookie = read(cookie)) !== undefined) {
|
|
|
|
|
result[name] = cookie;
|
|
|
|
|
}
|
|
|
|
|
@ -103,15 +106,16 @@
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 设置默认配置为空对象。
|
|
|
|
|
config.defaults = {};
|
|
|
|
|
|
|
|
|
|
// 定义$.removeCookie函数,用于删除cookie。
|
|
|
|
|
$.removeCookie = function (key, options) {
|
|
|
|
|
if ($.cookie(key) !== undefined) {
|
|
|
|
|
// Must not alter options, thus extending a fresh object...
|
|
|
|
|
// 一定不要改变options,因此扩展一个新对象...
|
|
|
|
|
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
})); // 插件结束
|