js.cookie.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*!
  2. * Javascript Cookie v1.5.1
  3. * https://github.com/js-cookie/js-cookie
  4. *
  5. * Copyright 2006, 2014 Klaus Hartl
  6. * Released under the MIT license
  7. */
  8. (function (factory) {
  9. var jQuery;
  10. if (typeof define === 'function' && define.amd) {
  11. // AMD (Register as an anonymous module)
  12. define(['jquery'], factory);
  13. } else if (typeof exports === 'object') {
  14. // Node/CommonJS
  15. try {
  16. jQuery = require('jquery');
  17. } catch(e) {}
  18. module.exports = factory(jQuery);
  19. } else {
  20. // Browser globals
  21. var _OldCookies = window.Cookies;
  22. var api = window.Cookies = factory(window.jQuery);
  23. api.noConflict = function() {
  24. window.Cookies = _OldCookies;
  25. return api;
  26. };
  27. }
  28. }(function ($) {
  29. var pluses = /\+/g;
  30. function encode(s) {
  31. return api.raw ? s : encodeURIComponent(s);
  32. }
  33. function decode(s) {
  34. return api.raw ? s : decodeURIComponent(s);
  35. }
  36. function stringifyCookieValue(value) {
  37. return encode(api.json ? JSON.stringify(value) : String(value));
  38. }
  39. function parseCookieValue(s) {
  40. if (s.indexOf('"') === 0) {
  41. // This is a quoted cookie as according to RFC2068, unescape...
  42. s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  43. }
  44. try {
  45. // Replace server-side written pluses with spaces.
  46. // If we can't decode the cookie, ignore it, it's unusable.
  47. // If we can't parse the cookie, ignore it, it's unusable.
  48. s = decodeURIComponent(s.replace(pluses, ' '));
  49. return api.json ? JSON.parse(s) : s;
  50. } catch(e) {}
  51. }
  52. function read(s, converter) {
  53. var value = api.raw ? s : parseCookieValue(s);
  54. return isFunction(converter) ? converter(value) : value;
  55. }
  56. function extend() {
  57. var key, options;
  58. var i = 0;
  59. var result = {};
  60. for (; i < arguments.length; i++) {
  61. options = arguments[ i ];
  62. for (key in options) {
  63. result[key] = options[key];
  64. }
  65. }
  66. return result;
  67. }
  68. function isFunction(obj) {
  69. return Object.prototype.toString.call(obj) === '[object Function]';
  70. }
  71. var api = function (key, value, options) {
  72. // Write
  73. if (arguments.length > 1 && !isFunction(value)) {
  74. options = extend(api.defaults, options);
  75. if (typeof options.expires === 'number') {
  76. var days = options.expires, t = options.expires = new Date();
  77. t.setMilliseconds(t.getMilliseconds() + days * 864e+5);
  78. }
  79. return (document.cookie = [
  80. encode(key), '=', stringifyCookieValue(value),
  81. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  82. options.path ? '; path=' + options.path : '',
  83. options.domain ? '; domain=' + options.domain : '',
  84. options.secure ? '; secure' : ''
  85. ].join(''));
  86. }
  87. // Read
  88. var result = key ? undefined : {},
  89. // To prevent the for loop in the first place assign an empty array
  90. // in case there are no cookies at all. Also prevents odd result when
  91. // calling "get()".
  92. cookies = document.cookie ? document.cookie.split('; ') : [],
  93. i = 0,
  94. l = cookies.length;
  95. for (; i < l; i++) {
  96. var parts = cookies[i].split('='),
  97. name = decode(parts.shift()),
  98. cookie = parts.join('=');
  99. if (key === name) {
  100. // If second argument (value) is a function it's a converter...
  101. result = read(cookie, value);
  102. break;
  103. }
  104. // Prevent storing a cookie that we couldn't decode.
  105. if (!key && (cookie = read(cookie)) !== undefined) {
  106. result[name] = cookie;
  107. }
  108. }
  109. return result;
  110. };
  111. api.get = api.set = api;
  112. api.defaults = {};
  113. api.remove = function (key, options) {
  114. // Must not alter options, thus extending a fresh object...
  115. api(key, '', extend(options, { expires: -1 }));
  116. return !api(key);
  117. };
  118. if ( $ ) {
  119. $.cookie = api;
  120. $.removeCookie = api.remove;
  121. }
  122. return api;
  123. }));