jquery.cookie.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*!
  2. * jQuery Cookie Plugin v1.4.1
  3. * https://github.com/carhartl/jquery-cookie
  4. *
  5. * Copyright 2006, 2014 Klaus Hartl
  6. * Released under the MIT license
  7. */
  8. (function (factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. // AMD (Register as an anonymous module)
  11. define(['jquery'], factory);
  12. } else if (typeof exports === 'object') {
  13. // Node/CommonJS
  14. module.exports = 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 (arguments.length > 1 && !$.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.setMilliseconds(t.getMilliseconds() + 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. cookies = document.cookie ? document.cookie.split('; ') : [],
  69. i = 0,
  70. l = cookies.length;
  71. for (; i < l; i++) {
  72. var parts = cookies[i].split('='),
  73. name = decode(parts.shift()),
  74. cookie = parts.join('=');
  75. if (key === name) {
  76. // If second argument (value) is a function it's a converter...
  77. result = read(cookie, value);
  78. break;
  79. }
  80. // Prevent storing a cookie that we couldn't decode.
  81. if (!key && (cookie = read(cookie)) !== undefined) {
  82. result[name] = cookie;
  83. }
  84. }
  85. return result;
  86. };
  87. config.defaults = {};
  88. $.removeCookie = function (key, options) {
  89. // Must not alter options, thus extending a fresh object...
  90. $.cookie(key, '', $.extend({}, options, { expires: -1 }));
  91. return !$.cookie(key);
  92. };
  93. }));