jquery.cookie.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. (function (factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as anonymous module.
  4. define(['jquery'], factory);
  5. } else {
  6. // Browser globals.
  7. factory(jQuery);
  8. }
  9. }(function ($) {
  10. var pluses = /\+/g;
  11. function encode(s) {
  12. return config.raw ? s : encodeURIComponent(s);
  13. }
  14. function decode(s) {
  15. return config.raw ? s : decodeURIComponent(s);
  16. }
  17. function stringifyCookieValue(value) {
  18. return encode(config.json ? JSON.stringify(value) : String(value));
  19. }
  20. function parseCookieValue(s) {
  21. if (s.indexOf('"') === 0) {
  22. // This is a quoted cookie as according to RFC2068, unescape...
  23. s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  24. }
  25. try {
  26. // Replace server-side written pluses with spaces.
  27. // If we can't decode the cookie, ignore it, it's unusable.
  28. s = decodeURIComponent(s.replace(pluses, ' '));
  29. } catch(e) {
  30. return;
  31. }
  32. try {
  33. // If we can't parse the cookie, ignore it, it's unusable.
  34. return config.json ? JSON.parse(s) : s;
  35. } catch(e) {}
  36. }
  37. function read(s, converter) {
  38. var value = config.raw ? s : parseCookieValue(s);
  39. return $.isFunction(converter) ? converter(value) : value;
  40. }
  41. var config = $.cookie = function (key, value, options) {
  42. // Write
  43. if (value !== undefined && !$.isFunction(value)) {
  44. options = $.extend({}, config.defaults, options);
  45. if (typeof options.expires === 'number') {
  46. var days = options.expires, t = options.expires = new Date();
  47. t.setDate(t.getDate() + days);
  48. }
  49. return (document.cookie = [
  50. encode(key), '=', stringifyCookieValue(value),
  51. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  52. options.path ? '; path=' + options.path : '',
  53. options.domain ? '; domain=' + options.domain : '',
  54. options.secure ? '; secure' : ''
  55. ].join(''));
  56. }
  57. // Read
  58. var result = key ? undefined : {};
  59. // To prevent the for loop in the first place assign an empty array
  60. // in case there are no cookies at all. Also prevents odd result when
  61. // calling $.cookie().
  62. var cookies = document.cookie ? document.cookie.split('; ') : [];
  63. for (var i = 0, l = cookies.length; i < l; i++) {
  64. var parts = cookies[i].split('=');
  65. var name = decode(parts.shift());
  66. var cookie = parts.join('=');
  67. if (key && key === name) {
  68. // If second argument (value) is a function it's a converter...
  69. result = read(cookie, value);
  70. break;
  71. }
  72. // Prevent storing a cookie that we couldn't decode.
  73. if (!key && (cookie = read(cookie)) !== undefined) {
  74. result[name] = cookie;
  75. }
  76. }
  77. return result;
  78. };
  79. config.defaults = {};
  80. $.removeCookie = function (key, options) {
  81. if ($.cookie(key) !== undefined) {
  82. // Must not alter options, thus extending a fresh object...
  83. $.cookie(key, '', $.extend({}, options, { expires: -1 }));
  84. return true;
  85. }
  86. return false;
  87. };
  88. }));