|
| 1 | +/*! |
| 2 | + * jQuery Cookie Plugin v1.3.1 |
| 3 | + * https://github.com/carhartl/jquery-cookie |
| 4 | + * |
| 5 | + * Copyright 2013 Klaus Hartl |
| 6 | + * Released under the MIT license |
| 7 | + */ |
| 8 | +(function (factory) { |
| 9 | + if (typeof define === 'function' && define.amd) { |
| 10 | + // AMD. Register as anonymous module. |
| 11 | + define(['jquery'], factory); |
| 12 | + } else { |
| 13 | + // Browser globals. |
| 14 | + factory(jQuery); |
| 15 | + } |
| 16 | +}(function ($) { |
| 17 | + |
| 18 | + var pluses = /\+/g; |
| 19 | + |
| 20 | + function raw(s) { |
| 21 | + return s; |
| 22 | + } |
| 23 | + |
| 24 | + function decoded(s) { |
| 25 | + return decodeURIComponent(s.replace(pluses, ' ')); |
| 26 | + } |
| 27 | + |
| 28 | + function converted(s) { |
| 29 | + if (s.indexOf('"') === 0) { |
| 30 | + // This is a quoted cookie as according to RFC2068, unescape |
| 31 | + s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); |
| 32 | + } |
| 33 | + try { |
| 34 | + return config.json ? JSON.parse(s) : s; |
| 35 | + } catch(er) {} |
| 36 | + } |
| 37 | + |
| 38 | + var config = $.cookie = function (key, value, options) { |
| 39 | + |
| 40 | + // write |
| 41 | + if (value !== undefined) { |
| 42 | + options = $.extend({}, config.defaults, options); |
| 43 | + |
| 44 | + if (typeof options.expires === 'number') { |
| 45 | + var days = options.expires, t = options.expires = new Date(); |
| 46 | + t.setDate(t.getDate() + days); |
| 47 | + } |
| 48 | + |
| 49 | + value = config.json ? JSON.stringify(value) : String(value); |
| 50 | + |
| 51 | + return (document.cookie = [ |
| 52 | + config.raw ? key : encodeURIComponent(key), |
| 53 | + '=', |
| 54 | + config.raw ? value : encodeURIComponent(value), |
| 55 | + options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
| 56 | + options.path ? '; path=' + options.path : '', |
| 57 | + options.domain ? '; domain=' + options.domain : '', |
| 58 | + options.secure ? '; secure' : '' |
| 59 | + ].join('')); |
| 60 | + } |
| 61 | + |
| 62 | + // read |
| 63 | + var decode = config.raw ? raw : decoded; |
| 64 | + var cookies = document.cookie.split('; '); |
| 65 | + var result = key ? undefined : {}; |
| 66 | + for (var i = 0, l = cookies.length; i < l; i++) { |
| 67 | + var parts = cookies[i].split('='); |
| 68 | + var name = decode(parts.shift()); |
| 69 | + var cookie = decode(parts.join('=')); |
| 70 | + |
| 71 | + if (key && key === name) { |
| 72 | + result = converted(cookie); |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + if (!key) { |
| 77 | + result[name] = converted(cookie); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return result; |
| 82 | + }; |
| 83 | + |
| 84 | + config.defaults = {}; |
| 85 | + |
| 86 | + $.removeCookie = function (key, options) { |
| 87 | + if ($.cookie(key) !== undefined) { |
| 88 | + // Must not alter options, thus extending a fresh object... |
| 89 | + $.cookie(key, '', $.extend({}, options, { expires: -1 })); |
| 90 | + return true; |
| 91 | + } |
| 92 | + return false; |
| 93 | + }; |
| 94 | + |
| 95 | +})); |
0 commit comments