cookie.js 3.1 KB

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