jquery-cookie.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*!
  2. jQuery Cookie Plugin
  3. Version: v1.4.1
  4. Plugin URL: https://github.com/carhartl/jquery-cookie
  5. License: Copyright 2006, 2014 Klaus Hartl | Released under the MIT license
  6. !*/
  7. (function (factory) {
  8. if (typeof define === 'function' && define.amd) {
  9. // AMD (Register as an anonymous module)
  10. define(['jquery'], factory);
  11. } else if (typeof exports === 'object') {
  12. // Node/CommonJS
  13. module.exports = factory(require('jquery'));
  14. } else {
  15. // Browser globals
  16. factory(jQuery);
  17. }
  18. }(function ($) {
  19. var pluses = /\+/g;
  20. function encode(s) {
  21. return config.raw ? s : encodeURIComponent(s);
  22. }
  23. function decode(s) {
  24. return config.raw ? s : decodeURIComponent(s);
  25. }
  26. function stringifyCookieValue(value) {
  27. return encode(config.json ? JSON.stringify(value) : String(value));
  28. }
  29. function parseCookieValue(s) {
  30. if (s.indexOf('"') === 0) {
  31. // This is a quoted cookie as according to RFC2068, unescape...
  32. s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  33. }
  34. try {
  35. // Replace server-side written pluses with spaces.
  36. // If we can't decode the cookie, ignore it, it's unusable.
  37. // If we can't parse the cookie, ignore it, it's unusable.
  38. s = decodeURIComponent(s.replace(pluses, ' '));
  39. return config.json ? JSON.parse(s) : s;
  40. } catch(e) {}
  41. }
  42. function read(s, converter) {
  43. var value = config.raw ? s : parseCookieValue(s);
  44. return $.isFunction(converter) ? converter(value) : value;
  45. }
  46. var config = $.cookie = function (key, value, options) {
  47. // Write
  48. if (arguments.length > 1 && !$.isFunction(value)) {
  49. options = $.extend({}, config.defaults, options);
  50. if (typeof options.expires === 'number') {
  51. var days = options.expires, t = options.expires = new Date();
  52. t.setMilliseconds(t.getMilliseconds() + days * 864e+5);
  53. }
  54. return (document.cookie = [
  55. encode(key), '=', stringifyCookieValue(value),
  56. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  57. options.path ? '; path=' + options.path : '',
  58. options.domain ? '; domain=' + options.domain : '',
  59. options.secure ? '; secure' : ''
  60. ].join(''));
  61. }
  62. // Read
  63. var result = key ? undefined : {},
  64. // To prevent the for loop in the first place assign an empty array
  65. // in case there are no cookies at all. Also prevents odd result when
  66. // calling $.cookie().
  67. cookies = document.cookie ? document.cookie.split('; ') : [],
  68. i = 0,
  69. l = cookies.length;
  70. for (; i < l; i++) {
  71. var parts = cookies[i].split('='),
  72. name = decode(parts.shift()),
  73. cookie = parts.join('=');
  74. if (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. // Must not alter options, thus extending a fresh object...
  89. $.cookie(key, '', $.extend({}, options, { expires: -1 }));
  90. return !$.cookie(key);
  91. };
  92. }));