You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
3.7 KiB

  1. /**
  2. * Cookie plugin
  3. *
  4. * Copyright (c) 2006 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. /**
  11. * Create a cookie with the given name and value and other optional parameters.
  12. *
  13. * @example $.cookie('the_cookie', 'the_value');
  14. * @desc Set the value of a cookie.
  15. * @example $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
  16. * @desc Create a cookie with all available options.
  17. * @example $.cookie('the_cookie', 'the_value');
  18. * @desc Create a session cookie.
  19. * @example $.cookie('the_cookie', null);
  20. * @desc Delete a cookie by passing null as value.
  21. *
  22. * @param String name The name of the cookie.
  23. * @param String value The value of the cookie.
  24. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
  25. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
  26. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
  27. * If set to null or omitted, the cookie will be a session cookie and will not be retained
  28. * when the the browser exits.
  29. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
  30. * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
  31. * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
  32. * require a secure protocol (like HTTPS).
  33. * @type undefined
  34. *
  35. * @name $.cookie
  36. * @cat Plugins/Cookie
  37. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  38. */
  39. /**
  40. * Get the value of a cookie with the given name.
  41. *
  42. * @example $.cookie('the_cookie');
  43. * @desc Get the value of a cookie.
  44. *
  45. * @param String name The name of the cookie.
  46. * @return The value of the cookie.
  47. * @type String
  48. *
  49. * @name $.cookie
  50. * @cat Plugins/Cookie
  51. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  52. */
  53. jQuery.cookie = function(name, value, options)
  54. {
  55. if (typeof value != 'undefined')
  56. { // name and value given, set cookie
  57. options = options || {};
  58. if (value === null)
  59. {
  60. value = '';
  61. options.expires = -1;
  62. }
  63. var expires = '';
  64. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString))
  65. {
  66. var date;
  67. if (typeof options.expires == 'number')
  68. {
  69. date = new Date();
  70. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  71. }
  72. else
  73. {
  74. date = options.expires;
  75. }
  76. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  77. }
  78. var path = options.path ? '; path=' + options.path : '';
  79. var domain = options.domain ? '; domain=' + options.domain : '';
  80. var secure = options.secure ? '; secure' : '';
  81. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  82. }
  83. else
  84. { // only name given, get cookie
  85. var cookieValue = null;
  86. if (document.cookie && document.cookie != '')
  87. {
  88. var cookies = document.cookie.split(';');
  89. for (var i = 0; i < cookies.length; i++)
  90. {
  91. var cookie = jQuery.trim(cookies[i]);
  92. // Does this cookie string begin with the name we want?
  93. if (cookie.substring(0, name.length + 1) == (name + '='))
  94. {
  95. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  96. break;
  97. }
  98. }
  99. }
  100. return cookieValue;
  101. }
  102. };