jquery.cookie.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. s = decodeURIComponent(s.replace(pluses, ' '));
  36. } catch(e) {
  37. return;
  38. }
  39. try {
  40. // If we can't parse the cookie, ignore it, it's unusable.
  41. return config.json ? JSON.parse(s) : s;
  42. } catch(e) {}
  43. }
  44. function read(s, converter) {
  45. var value = config.raw ? s : parseCookieValue(s);
  46. return $.isFunction(converter) ? converter(value) : value;
  47. }
  48. var config = $.cookie = function (key, value, options) {
  49. // Write
  50. if (value !== undefined && !$.isFunction(value)) {
  51. options = $.extend({}, config.defaults, options);
  52. if (typeof options.expires === 'number') {
  53. var days = options.expires, t = options.expires = new Date();
  54. t.setDate(t.getDate() + days);
  55. }
  56. return (document.cookie = [
  57. encode(key), '=', stringifyCookieValue(value),
  58. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  59. options.path ? '; path=' + options.path : '',
  60. options.domain ? '; domain=' + options.domain : '',
  61. options.secure ? '; secure' : ''
  62. ].join(''));
  63. }
  64. // Read
  65. var result = key ? undefined : {};
  66. // To prevent the for loop in the first place assign an empty array
  67. // in case there are no cookies at all. Also prevents odd result when
  68. // calling $.cookie().
  69. var cookies = document.cookie ? document.cookie.split('; ') : [];
  70. for (var i = 0, l = cookies.length; i < l; i++) {
  71. var parts = cookies[i].split('=');
  72. var name = decode(parts.shift());
  73. var cookie = parts.join('=');
  74. if (key && key === name) {
  75. // If second argument (value) is a function it's a converter...
  76. result = read(cookie, value);
  77. break;
  78. }
  79. // Prevent storing a cookie that we couldn't decode.
  80. if (!key && (cookie = read(cookie)) !== undefined) {
  81. result[name] = cookie;
  82. }
  83. }
  84. return result;
  85. };
  86. config.defaults = {};
  87. $.removeCookie = function (key, options) {
  88. if ($.cookie(key) !== undefined) {
  89. // Must not alter options, thus extending a fresh object...
  90. $.cookie(key, '', $.extend({}, options, { expires: -1 }));
  91. return true;
  92. }
  93. return false;
  94. };
  95. }));