jquery.cookie.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*!
  2. * jQuery Cookie Plugin v1.3.1
  3. * https://github.com/carhartl/jquery-cookie
  4. *
  5. * Copyright 2013 Klaus Hartl
  6. * Released under the MIT license
  7. */
  8. (function (factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. // AMD. Register as anonymous module.
  11. define(['jquery'], factory);
  12. } else {
  13. // Browser globals.
  14. factory(jQuery);
  15. }
  16. }(function ($) {
  17. var pluses = /\+/g;
  18. function decode(s) {
  19. if (config.raw) {
  20. return s;
  21. }
  22. try {
  23. // If we can't decode the cookie, ignore it, it's unusable.
  24. return decodeURIComponent(s.replace(pluses, ' '));
  25. } catch(e) {}
  26. }
  27. function decodeAndParse(s) {
  28. if (s.indexOf('"') === 0) {
  29. // This is a quoted cookie as according to RFC2068, unescape...
  30. s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  31. }
  32. s = decode(s);
  33. try {
  34. // If we can't parse the cookie, ignore it, it's unusable.
  35. return config.json ? JSON.parse(s) : s;
  36. } catch(e) {}
  37. }
  38. var config = $.cookie = function (key, value, options) {
  39. // Write
  40. if (value !== undefined) {
  41. options = $.extend({}, config.defaults, options);
  42. if (typeof options.expires === 'number') {
  43. var days = options.expires, t = options.expires = new Date();
  44. t.setDate(t.getDate() + days);
  45. }
  46. value = config.json ? JSON.stringify(value) : String(value);
  47. return (document.cookie = [
  48. config.raw ? key : encodeURIComponent(key),
  49. '=',
  50. config.raw ? value : encodeURIComponent(value),
  51. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  52. options.path ? '; path=' + options.path : '',
  53. options.domain ? '; domain=' + options.domain : '',
  54. options.secure ? '; secure' : ''
  55. ].join(''));
  56. }
  57. // Read
  58. var result = key ? undefined : {};
  59. // To prevent the for loop in the first place assign an empty array
  60. // in case there are no cookies at all. Also prevents odd result when
  61. // calling $.cookie().
  62. var cookies = document.cookie ? document.cookie.split('; ') : [];
  63. for (var i = 0, l = cookies.length; i < l; i++) {
  64. var parts = cookies[i].split('=');
  65. var name = decode(parts.shift());
  66. var cookie = parts.join('=');
  67. if (key && key === name) {
  68. result = decodeAndParse(cookie);
  69. break;
  70. }
  71. // Prevent storing a cookie that we couldn't decode.
  72. if (!key && (cookie = decodeAndParse(cookie)) !== undefined) {
  73. result[name] = cookie;
  74. }
  75. }
  76. return result;
  77. };
  78. config.defaults = {};
  79. $.removeCookie = function (key, options) {
  80. if ($.cookie(key) !== undefined) {
  81. // Must not alter options, thus extending a fresh object...
  82. $.cookie(key, '', $.extend({}, options, { expires: -1 }));
  83. return true;
  84. }
  85. return false;
  86. };
  87. }));