jquery.cookie.js 3.2 KB

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