jquery.cookie.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * jQuery Cookie plugin
  3. *
  4. * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
  5. * Dual licensed under the MIT and GPL licenses:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. * http://www.gnu.org/licenses/gpl.html
  8. *
  9. */
  10. jQuery.cookie = function (key, value, options) {
  11. // key and at least value given, set cookie...
  12. if (arguments.length > 1 && String(value) !== "[object Object]") {
  13. options = jQuery.extend({}, options);
  14. if (value === null || value === undefined) {
  15. options.expires = -1;
  16. }
  17. if (typeof options.expires === 'number') {
  18. var days = options.expires, t = options.expires = new Date();
  19. t.setDate(t.getDate() + days);
  20. }
  21. value = String(value);
  22. return (document.cookie = [
  23. encodeURIComponent(key), '=',
  24. options.raw ? value : encodeURIComponent(value),
  25. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  26. options.path ? '; path=' + options.path : '',
  27. options.domain ? '; domain=' + options.domain : '',
  28. options.secure ? '; secure' : ''
  29. ].join(''));
  30. }
  31. // key and possibly options given, get cookie...
  32. options = value || {};
  33. var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
  34. return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
  35. };