js.cookie.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*!
  2. * JavaScript Cookie v2.0.4
  3. * https://github.com/js-cookie/js-cookie
  4. *
  5. * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
  6. * Released under the MIT license
  7. */
  8. (function (factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. define(factory);
  11. } else if (typeof exports === 'object') {
  12. module.exports = factory();
  13. } else {
  14. var _OldCookies = window.Cookies;
  15. var api = window.Cookies = factory();
  16. api.noConflict = function () {
  17. window.Cookies = _OldCookies;
  18. return api;
  19. };
  20. }
  21. }(function () {
  22. function extend () {
  23. var i = 0;
  24. var result = {};
  25. for (; i < arguments.length; i++) {
  26. var attributes = arguments[ i ];
  27. for (var key in attributes) {
  28. result[key] = attributes[key];
  29. }
  30. }
  31. return result;
  32. }
  33. function init (converter) {
  34. function api (key, value, attributes) {
  35. var result;
  36. // Write
  37. if (arguments.length > 1) {
  38. attributes = extend({
  39. path: '/'
  40. }, api.defaults, attributes);
  41. if (typeof attributes.expires === 'number') {
  42. var expires = new Date();
  43. expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
  44. attributes.expires = expires;
  45. }
  46. try {
  47. result = JSON.stringify(value);
  48. if (/^[\{\[]/.test(result)) {
  49. value = result;
  50. }
  51. } catch (e) {}
  52. if (!converter.write) {
  53. value = encodeURIComponent(String(value))
  54. .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
  55. } else {
  56. value = converter.write(value, key);
  57. }
  58. key = encodeURIComponent(String(key));
  59. key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
  60. key = key.replace(/[\(\)]/g, escape);
  61. return (document.cookie = [
  62. key, '=', value,
  63. attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
  64. attributes.path && '; path=' + attributes.path,
  65. attributes.domain && '; domain=' + attributes.domain,
  66. attributes.secure ? '; secure' : ''
  67. ].join(''));
  68. }
  69. // Read
  70. if (!key) {
  71. result = {};
  72. }
  73. // To prevent the for loop in the first place assign an empty array
  74. // in case there are no cookies at all. Also prevents odd result when
  75. // calling "get()"
  76. var cookies = document.cookie ? document.cookie.split('; ') : [];
  77. var rdecode = /(%[0-9A-Z]{2})+/g;
  78. var i = 0;
  79. for (; i < cookies.length; i++) {
  80. var parts = cookies[i].split('=');
  81. var name = parts[0].replace(rdecode, decodeURIComponent);
  82. var cookie = parts.slice(1).join('=');
  83. if (cookie.charAt(0) === '"') {
  84. cookie = cookie.slice(1, -1);
  85. }
  86. try {
  87. cookie = converter.read ?
  88. converter.read(cookie, name) : converter(cookie, name) ||
  89. cookie.replace(rdecode, decodeURIComponent);
  90. if (this.json) {
  91. try {
  92. cookie = JSON.parse(cookie);
  93. } catch (e) {}
  94. }
  95. if (key === name) {
  96. result = cookie;
  97. break;
  98. }
  99. if (!key) {
  100. result[name] = cookie;
  101. }
  102. } catch (e) {}
  103. }
  104. return result;
  105. }
  106. api.get = api.set = api;
  107. api.getJSON = function () {
  108. return api.apply({
  109. json: true
  110. }, [].slice.call(arguments));
  111. };
  112. api.defaults = {};
  113. api.remove = function (key, attributes) {
  114. api(key, '', extend(attributes, {
  115. expires: -1
  116. }));
  117. };
  118. api.withConverter = init;
  119. return api;
  120. }
  121. return init(function () {});
  122. }));