js.cookie.js 3.8 KB

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