An open source, stand-alone, customisable public spending data web app.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

jquery.cookie.js 8.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. Keep in mind that you have to use the same path and domain
  21. * used when the cookie was set.
  22. *
  23. * @param String name The name of the cookie.
  24. * @param String value The value of the cookie.
  25. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
  26. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
  27. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
  28. * If set to null or omitted, the cookie will be a session cookie and will not be retained
  29. * when the the browser exits.
  30. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
  31. * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
  32. * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
  33. * require a secure protocol (like HTTPS).
  34. * @type undefined
  35. *
  36. * @name $.cookie
  37. * @cat Plugins/Cookie
  38. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  39. */
  40. /**
  41. * Get the value of a cookie with the given name.
  42. *
  43. * @example $.cookie('the_cookie');
  44. * @desc Get the value of a cookie.
  45. *
  46. * @param String name The name of the cookie.
  47. * @return The value of the cookie.
  48. * @type String
  49. *
  50. * @name $.cookie
  51. * @cat Plugins/Cookie
  52. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  53. */
  54. jQuery.cookie = function(name, value, options) {
  55. if (typeof value != 'undefined') { // name and value given, set cookie
  56. options = options || {};
  57. if (value === null) {
  58. value = '';
  59. options.expires = -1;
  60. }
  61. var expires = '';
  62. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  63. var date;
  64. if (typeof options.expires == 'number') {
  65. date = new Date();
  66. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  67. } else {
  68. date = options.expires;
  69. }
  70. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  71. }
  72. // CAUTION: Needed to parenthesize options.path and options.domain
  73. // in the following expressions, otherwise they evaluate to undefined
  74. // in the packed version for some reason...
  75. var path = options.path ? '; path=' + (options.path) : '';
  76. var domain = options.domain ? '; domain=' + (options.domain) : '';
  77. var secure = options.secure ? '; secure' : '';
  78. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  79. } else { // only name given, get cookie
  80. var cookieValue = null;
  81. if (document.cookie && document.cookie != '') {
  82. var cookies = document.cookie.split(';');
  83. for (var i = 0; i < cookies.length; i++) {
  84. var cookie = jQuery.trim(cookies[i]);
  85. // Does this cookie string begin with the name we want?
  86. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  87. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  88. break;
  89. }
  90. }
  91. }
  92. return cookieValue;
  93. }
  94. };/**
  95. * Cookie plugin
  96. *
  97. * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
  98. * Dual licensed under the MIT and GPL licenses:
  99. * http://www.opensource.org/licenses/mit-license.php
  100. * http://www.gnu.org/licenses/gpl.html
  101. *
  102. */
  103. /**
  104. * Create a cookie with the given name and value and other optional parameters.
  105. *
  106. * @example $.cookie('the_cookie', 'the_value');
  107. * @desc Set the value of a cookie.
  108. * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
  109. * @desc Create a cookie with all available options.
  110. * @example $.cookie('the_cookie', 'the_value');
  111. * @desc Create a session cookie.
  112. * @example $.cookie('the_cookie', null);
  113. * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
  114. * used when the cookie was set.
  115. *
  116. * @param String name The name of the cookie.
  117. * @param String value The value of the cookie.
  118. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
  119. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
  120. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
  121. * If set to null or omitted, the cookie will be a session cookie and will not be retained
  122. * when the the browser exits.
  123. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
  124. * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
  125. * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
  126. * require a secure protocol (like HTTPS).
  127. * @type undefined
  128. *
  129. * @name $.cookie
  130. * @cat Plugins/Cookie
  131. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  132. */
  133. /**
  134. * Get the value of a cookie with the given name.
  135. *
  136. * @example $.cookie('the_cookie');
  137. * @desc Get the value of a cookie.
  138. *
  139. * @param String name The name of the cookie.
  140. * @return The value of the cookie.
  141. * @type String
  142. *
  143. * @name $.cookie
  144. * @cat Plugins/Cookie
  145. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  146. */
  147. jQuery.cookie = function(name, value, options) {
  148. if (typeof value != 'undefined') { // name and value given, set cookie
  149. options = options || {};
  150. if (value === null) {
  151. value = '';
  152. options.expires = -1;
  153. }
  154. var expires = '';
  155. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  156. var date;
  157. if (typeof options.expires == 'number') {
  158. date = new Date();
  159. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  160. } else {
  161. date = options.expires;
  162. }
  163. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  164. }
  165. // CAUTION: Needed to parenthesize options.path and options.domain
  166. // in the following expressions, otherwise they evaluate to undefined
  167. // in the packed version for some reason...
  168. var path = options.path ? '; path=' + (options.path) : '';
  169. var domain = options.domain ? '; domain=' + (options.domain) : '';
  170. var secure = options.secure ? '; secure' : '';
  171. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  172. } else { // only name given, get cookie
  173. var cookieValue = null;
  174. if (document.cookie && document.cookie != '') {
  175. var cookies = document.cookie.split(';');
  176. for (var i = 0; i < cookies.length; i++) {
  177. var cookie = jQuery.trim(cookies[i]);
  178. // Does this cookie string begin with the name we want?
  179. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  180. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  181. break;
  182. }
  183. }
  184. }
  185. return cookieValue;
  186. }
  187. };