2 * jQuery Cookie Plugin v1.4.1
3 * https://github.com/carhartl/jquery-cookie
5 * Copyright 2006, 2014 Klaus Hartl
6 * Released under the MIT license
9 if (typeof define === 'function' && define.amd) {
10 // AMD (Register as an anonymous module)
11 define(['jquery'], factory);
12 } else if (typeof exports === 'object') {
14 module.exports = factory(require('jquery'));
24 return config.raw ? s : encodeURIComponent(s);
28 return config.raw ? s : decodeURIComponent(s);
31 function stringifyCookieValue(value) {
32 return encode(config.json ? JSON.stringify(value) : String(value));
35 function parseCookieValue(s) {
36 if (s.indexOf('"') === 0) {
37 // This is a quoted cookie as according to RFC2068, unescape...
38 s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
42 // Replace server-side written pluses with spaces.
43 // If we can't decode the cookie, ignore it, it's unusable.
44 // If we can't parse the cookie, ignore it, it's unusable.
45 s = decodeURIComponent(s.replace(pluses, ' '));
46 return config.json ? JSON.parse(s) : s;
50 function read(s, converter) {
51 var value = config.raw ? s : parseCookieValue(s);
52 return $.isFunction(converter) ? converter(value) : value;
55 var config = $.cookie = function (key, value, options) {
59 if (arguments.length > 1 && !$.isFunction(value)) {
60 options = $.extend({}, config.defaults, options);
62 if (typeof options.expires === 'number') {
63 var days = options.expires, t = options.expires = new Date();
64 t.setMilliseconds(t.getMilliseconds() + days * 864e+5);
67 return (document.cookie = [
68 encode(key), '=', stringifyCookieValue(value),
69 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
70 options.path ? '; path=' + options.path : '',
71 options.domain ? '; domain=' + options.domain : '',
72 options.secure ? '; secure' : ''
78 var result = key ? undefined : {},
79 // To prevent the for loop in the first place assign an empty array
80 // in case there are no cookies at all. Also prevents odd result when
81 // calling $.cookie().
82 cookies = document.cookie ? document.cookie.split('; ') : [],
87 var parts = cookies[i].split('='),
88 name = decode(parts.shift()),
89 cookie = parts.join('=');
92 // If second argument (value) is a function it's a converter...
93 result = read(cookie, value);
97 // Prevent storing a cookie that we couldn't decode.
98 if (!key && (cookie = read(cookie)) !== undefined) {
99 result[name] = cookie;
106 config.defaults = {};
108 $.removeCookie = function (key, options) {
109 // Must not alter options, thus extending a fresh object...
110 $.cookie(key, '', $.extend({}, options, { expires: -1 }));
111 return !$.cookie(key);