2 * jQuery Cookie Plugin v1.4.0
3 * https://github.com/carhartl/jquery-cookie
5 * Copyright 2013 Klaus Hartl
6 * Released under the MIT license
9 if (typeof define === 'function' && define.amd) {
10 // AMD. Register as anonymous module.
11 define(['jquery'], factory);
21 return config.raw ? s : encodeURIComponent(s);
25 return config.raw ? s : decodeURIComponent(s);
28 function stringifyCookieValue(value) {
29 return encode(config.json ? JSON.stringify(value) : String(value));
32 function parseCookieValue(s) {
33 if (s.indexOf('"') === 0) {
34 // This is a quoted cookie as according to RFC2068, unescape...
35 s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
39 // Replace server-side written pluses with spaces.
40 // If we can't decode the cookie, ignore it, it's unusable.
41 // If we can't parse the cookie, ignore it, it's unusable.
42 s = decodeURIComponent(s.replace(pluses, ' '));
43 return config.json ? JSON.parse(s) : s;
47 function read(s, converter) {
48 var value = config.raw ? s : parseCookieValue(s);
49 return $.isFunction(converter) ? converter(value) : value;
52 var config = $.cookie = function (key, value, options) {
56 if (value !== undefined && !$.isFunction(value)) {
57 options = $.extend({}, config.defaults, options);
59 if (typeof options.expires === 'number') {
60 var days = options.expires, t = options.expires = new Date();
61 t.setTime(+t + days * 864e+5);
64 return (document.cookie = [
65 encode(key), '=', stringifyCookieValue(value),
66 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
67 options.path ? '; path=' + options.path : '',
68 options.domain ? '; domain=' + options.domain : '',
69 options.secure ? '; secure' : ''
75 var result = key ? undefined : {};
77 // To prevent the for loop in the first place assign an empty array
78 // in case there are no cookies at all. Also prevents odd result when
79 // calling $.cookie().
80 var cookies = document.cookie ? document.cookie.split('; ') : [];
82 for (var i = 0, l = cookies.length; i < l; i++) {
83 var parts = cookies[i].split('=');
84 var name = decode(parts.shift());
85 var cookie = parts.join('=');
87 if (key && key === name) {
88 // If second argument (value) is a function it's a converter...
89 result = read(cookie, value);
93 // Prevent storing a cookie that we couldn't decode.
94 if (!key && (cookie = read(cookie)) !== undefined) {
95 result[name] = cookie;
102 config.defaults = {};
104 $.removeCookie = function (key, options) {
105 if ($.cookie(key) === undefined) {
109 // Must not alter options, thus extending a fresh object...
110 $.cookie(key, '', $.extend({}, options, { expires: -1 }));
111 return !$.cookie(key);