jquery.cookie.js.下载 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*!
  2. * jQuery Cookie Plugin v1.3
  3. * https://github.com/carhartl/jquery-cookie
  4. *
  5. * Copyright 2011, Klaus Hartl
  6. * Dual licensed under the MIT or GPL Version 2 licenses.
  7. * http://www.opensource.org/licenses/mit-license.php
  8. * http://www.opensource.org/licenses/GPL-2.0
  9. */
  10. (function ($, document, undefined) {
  11. var pluses = /\+/g;
  12. function raw(s) {
  13. return s;
  14. }
  15. function decoded(s) {
  16. return decodeURIComponent(s.replace(pluses, ' '));
  17. }
  18. var config = $.cookie = function (key, value, options) {
  19. // write
  20. if (value !== undefined) {
  21. options = $.extend({}, config.defaults, options);
  22. if (value === null) {
  23. options.expires = -1;
  24. }
  25. if (typeof options.expires === 'number') {
  26. var days = options.expires, t = options.expires = new Date();
  27. t.setDate(t.getDate() + days);
  28. }
  29. value = config.json ? JSON.stringify(value) : String(value);
  30. return (document.cookie = [
  31. encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
  32. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  33. options.path ? '; path=' + options.path : '',
  34. options.domain ? '; domain=' + options.domain : '',
  35. options.secure ? '; secure' : ''
  36. ].join(''));
  37. }
  38. // read
  39. var decode = config.raw ? raw : decoded;
  40. var cookies = document.cookie.split('; ');
  41. for (var i = 0, l = cookies.length; i < l; i++) {
  42. var parts = cookies[i].split('=');
  43. if (decode(parts.shift()) === key) {
  44. var cookie = decode(parts.join('='));
  45. return config.json ? JSON.parse(cookie) : cookie;
  46. }
  47. }
  48. return null;
  49. };
  50. config.defaults = {};
  51. $.removeCookie = function (key, options) {
  52. if ($.cookie(key) !== null) {
  53. $.cookie(key, null, options);
  54. return true;
  55. }
  56. return false;
  57. };
  58. })(jQuery, document);