PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/jquery-cookie/jquery.cookie.js

http://github.com/silverstripe/sapphire
JavaScript | 97 lines | 38 code | 2 blank | 57 comment | 18 complexity | a59b7dd7fa29688050391fecd2ab33d9 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  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 = jQuery.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
  60. options.expires = -1;
  61. }
  62. var expires = '';
  63. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  64. var date;
  65. if (typeof options.expires == 'number') {
  66. date = new Date();
  67. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  68. } else {
  69. date = options.expires;
  70. }
  71. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  72. }
  73. // NOTE Needed to parenthesize options.path and options.domain
  74. // in the following expressions, otherwise they evaluate to undefined
  75. // in the packed version for some reason...
  76. var path = options.path ? '; path=' + (options.path) : '';
  77. var domain = options.domain ? '; domain=' + (options.domain) : '';
  78. var secure = options.secure ? '; secure' : '';
  79. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  80. } else { // only name given, get cookie
  81. var cookieValue = null;
  82. if (document.cookie && document.cookie != '') {
  83. var cookies = document.cookie.split(';');
  84. for (var i = 0; i < cookies.length; i++) {
  85. var cookie = jQuery.trim(cookies[i]);
  86. // Does this cookie string begin with the name we want?
  87. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  88. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  89. break;
  90. }
  91. }
  92. }
  93. return cookieValue;
  94. }
  95. };