jquery.cookie.js 3.0 KB

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