| 1 |
// jQuery cookie plugin |
| 2 |
(function (factory) { |
| 3 |
if (typeof define === 'function' && define.amd) { |
| 4 |
define(['jquery'], factory); |
| 5 |
} else if (typeof exports === 'object') { |
| 6 |
module.exports = factory(require('jquery')); |
| 7 |
} else { |
| 8 |
factory(jQuery); |
| 9 |
} |
| 10 |
}(function ($) { |
| 11 |
var pluses = /\+/g; |
| 12 |
function encode(s) { |
| 13 |
return config.raw ? s : encodeURIComponent(s); |
| 14 |
} |
| 15 |
function decode(s) { |
| 16 |
return config.raw ? s : decodeURIComponent(s); |
| 17 |
} |
| 18 |
function stringifyCookieValue(value) { |
| 19 |
return encode(config.json ? JSON.stringify(value) : String(value)); |
| 20 |
} |
| 21 |
function parseCookieValue(s) { |
| 22 |
if (s.indexOf('"') === 0) { |
| 23 |
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); |
| 24 |
} |
| 25 |
try { |
| 26 |
s = decodeURIComponent(s.replace(pluses, ' ')); |
| 27 |
return config.json ? JSON.parse(s) : s; |
| 28 |
} catch (e) { |
| 29 |
} |
| 30 |
} |
| 31 |
function read(s, converter) { |
| 32 |
var value = config.raw ? s : parseCookieValue(s); |
| 33 |
return $.isFunction(converter) ? converter(value) : value; |
| 34 |
} |
| 35 |
var config = $.cookie = function (key, value, options) { |
| 36 |
if (arguments.length > 1 && !$.isFunction(value)) { |
| 37 |
options = $.extend({}, config.defaults, options); |
| 38 |
if (typeof options.expires === 'number') { |
| 39 |
var days = options.expires, t = options.expires = new Date(); |
| 40 |
t.setMilliseconds(t.getMilliseconds() + days * 864e+5); |
| 41 |
} |
| 42 |
return (document.cookie = [ |
| 43 |
encode(key), '=', stringifyCookieValue(value), |
| 44 |
options.expires ? '; expires=' + options.expires.toUTCString() : '', |
| 45 |
options.path ? '; path=' + options.path : '', |
| 46 |
options.domain ? '; domain=' + options.domain : '', |
| 47 |
options.secure ? '; secure' : '' |
| 48 |
].join('')); |
| 49 |
} |
| 50 |
var result = key ? undefined : {}, |
| 51 |
cookies = document.cookie ? document.cookie.split('; ') : [], |
| 52 |
i = 0, |
| 53 |
l = cookies.length; |
| 54 |
for (; i < l; i++) { |
| 55 |
var parts = cookies[i].split('='), |
| 56 |
name = decode(parts.shift()), |
| 57 |
cookie = parts.join('='); |
| 58 |
if (key === name) { |
| 59 |
result = read(cookie, value); |
| 60 |
break; |
| 61 |
} |
| 62 |
if (!key && (cookie = read(cookie)) !== undefined) { |
| 63 |
result[name] = cookie; |
| 64 |
} |
| 65 |
} |
| 66 |
return result; |
| 67 |
}; |
| 68 |
config.defaults = {}; |
| 69 |
$.removeCookie = function (key, options) { |
| 70 |
$.cookie(key, '', $.extend({}, options, {expires: -1})); |
| 71 |
return !$.cookie(key); |
| 72 |
}; |
| 73 |
})); |