jquery.cookie.js 2.8 KB

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