2 * jQuery Cookie Plugin v1.3.1
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);
25 // If we can't decode the cookie, ignore it, it's unusable.
26 return decodeURIComponent(s.replace(pluses, ' '));
30 function decodeAndParse(s) {
31 if (s.indexOf('"') === 0) {
32 // This is a quoted cookie as according to RFC2068, unescape...
33 s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
39 // If we can't parse the cookie, ignore it, it's unusable.
40 return config.json ? JSON.parse(s) : s;
44 var config = $.cookie = function (key, value, options) {
47 if (value !== undefined) {
48 options = $.extend({}, config.defaults, options);
50 if (typeof options.expires === 'number') {
51 var days = options.expires, t = options.expires = new Date();
52 t.setDate(t.getDate() + days);
55 value = config.json ? JSON.stringify(value) : String(value);
57 return (document.cookie = [
58 config.raw ? key : encodeURIComponent(key),
60 config.raw ? value : encodeURIComponent(value),
61 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
62 options.path ? '; path=' + options.path : '',
63 options.domain ? '; domain=' + options.domain : '',
64 options.secure ? '; secure' : ''
70 var result = key ? undefined : {};
72 // To prevent the for loop in the first place assign an empty array
73 // in case there are no cookies at all. Also prevents odd result when
74 // calling $.cookie().
75 var cookies = document.cookie ? document.cookie.split('; ') : [];
77 for (var i = 0, l = cookies.length; i < l; i++) {
78 var parts = cookies[i].split('=');
79 var name = decode(parts.shift());
80 var cookie = parts.join('=');
82 if (key && key === name) {
83 result = decodeAndParse(cookie);
87 // Prevent storing a cookie that we couldn't decode.
88 if (!key && (cookie = decodeAndParse(cookie)) !== undefined) {
89 result[name] = cookie;
98 $.removeCookie = function (key, options) {
99 if ($.cookie(key) !== undefined) {
100 // Must not alter options, thus extending a fresh object...
101 $.cookie(key, '', $.extend({}, options, { expires: -1 }));