jquery.cookie.js 1.2 KB

123456789101112131415161718192021222324252627282930
  1. jQuery.cookie = function (key, value, options) {
  2. // key and value given, set cookie...
  3. if (arguments.length > 1 && (value === null || typeof value !== "object")) {
  4. options = jQuery.extend({}, options);
  5. if (value === null) {
  6. options.expires = -1;
  7. }
  8. if (typeof options.expires === 'number') {
  9. var days = options.expires, t = options.expires = new Date();
  10. t.setDate(t.getDate() + days);
  11. }
  12. return (document.cookie = [
  13. encodeURIComponent(key), '=',
  14. options.raw ? String(value) : encodeURIComponent(String(value)),
  15. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  16. options.path ? '; path=' + options.path : '',
  17. options.domain ? '; domain=' + options.domain : '',
  18. options.secure ? '; secure' : ''
  19. ].join(''));
  20. }
  21. // key and possibly options given, get cookie...
  22. options = value || {};
  23. var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
  24. return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
  25. };