jquery.cookie.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*jslint browser: true */ /*global jQuery: true */
  2. /**
  3. * jQuery Cookie plugin
  4. *
  5. * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
  6. * Dual licensed under the MIT and GPL licenses:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. * http://www.gnu.org/licenses/gpl.html
  9. *
  10. */
  11. // TODO JsDoc
  12. /**
  13. * Create a cookie with the given key and value and other optional parameters.
  14. *
  15. * @example $.cookie('the_cookie', 'the_value');
  16. * @desc Set the value of a cookie.
  17. * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
  18. * @desc Create a cookie with all available options.
  19. * @example $.cookie('the_cookie', 'the_value');
  20. * @desc Create a session cookie.
  21. * @example $.cookie('the_cookie', null);
  22. * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
  23. * used when the cookie was set.
  24. *
  25. * @param String key The key of the cookie.
  26. * @param String value The value of the cookie.
  27. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
  28. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
  29. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
  30. * If set to null or omitted, the cookie will be a session cookie and will not be retained
  31. * when the the browser exits.
  32. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
  33. * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
  34. * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
  35. * require a secure protocol (like HTTPS).
  36. * @type undefined
  37. *
  38. * @name $.cookie
  39. * @cat Plugins/Cookie
  40. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  41. */
  42. /**
  43. * Get the value of a cookie with the given key.
  44. *
  45. * @example $.cookie('the_cookie');
  46. * @desc Get the value of a cookie.
  47. *
  48. * @param String key The key of the cookie.
  49. * @return The value of the cookie.
  50. * @type String
  51. *
  52. * @name $.cookie
  53. * @cat Plugins/Cookie
  54. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  55. */
  56. jQuery.cookie = function (key, value, options) {
  57. // key and value given, set cookie...
  58. if (arguments.length > 1 && (value === null || typeof value !== "object")) {
  59. options = jQuery.extend({}, options);
  60. if (value === null) {
  61. options.expires = -1;
  62. }
  63. if (typeof options.expires === 'number') {
  64. var days = options.expires, t = options.expires = new Date();
  65. t.setDate(t.getDate() + days);
  66. }
  67. return (document.cookie = [
  68. encodeURIComponent(key), '=',
  69. options.raw ? String(value) : encodeURIComponent(String(value)),
  70. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  71. options.path ? '; path=' + options.path : '',
  72. options.domain ? '; domain=' + options.domain : '',
  73. options.secure ? '; secure' : ''
  74. ].join(''));
  75. }
  76. // key and possibly options given, get cookie...
  77. options = value || {};
  78. var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
  79. return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
  80. };