jquery.cookie.js 是一款轻量级的 cookie 插件,可以读取,写入和删除 cookie。本文主要针对 jquery.cookie.js 的用法进行详细的介绍。
使用方法:
设置 cookie:
1. $.cookie('the_cookie', 'the_value');
复制代码
注:如果 $.cookie 没有第三个参数,那么当浏览器关闭时,该 cookie 将会自动删除。
设置一个有效期为 7 天的 cookie:
1. $.cookie('the_cookie', 'the_value', {expires: 7});
复制代码
注:$.cookie 第三个参数是一个对象,除了可以设置有效期(expires: 7),还可以设置有效路径(path: '/')、有效域(domain: 'jquery.com')及安全性(secure: true)。
读取 cookie:
1. $.cookie('the_cookie');
复制代码
注:如果没有该 cookie,返回 null。
删除 cookie:
1. $.cookie('the_cookie', null);
复制代码
我们只需要给需要删除的 cookie 设置为 null,就可以删除该 cookie。
最后附上源代码:
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. /**
12. * Create a cookie with the given name and value and other optional parameters.
13. *
14. * @example $.cookie('the_cookie', 'the_value');
15. * @desc Set the value of a cookie.
16. * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
17. * @desc Create a cookie with all available options.
18. * @example $.cookie('the_cookie', 'the_value');
19. * @desc Create a session cookie.
20. * @example $.cookie('the_cookie', null);
21. * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
22. * used when the cookie was set.
23. *
24. * @param String name The name of the cookie.
25. * @param String value The value of the cookie.
26. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
27. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
28. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
29. * If set to null or omitted, the cookie will be a session cookie and will not be retained
30. * when the the browser exits.
31. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
32. * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
33. * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
34. * require a secure protocol (like HTTPS).
35. * @type undefined
36. *
37. * @name $.cookie
38. * @cat Plugins/Cookie
39. * @author Klaus Hartl/klaus.hartl@stilbuero.de
40. */
41.
42. /**
43. * Get the value of a cookie with the given name.
44. *
45. * @example $.cookie('the_cookie');
46. * @desc Get the value of a cookie.
47. *
48. * @param String name The name 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(name, value, options) {
57. if (typeof value != 'undefined') { // name and value given, set cookie
58. options = options || {};
59. if (value === null) {
60. value = '';
61. options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
62. options.expires = -1;
63. }
64. var expires = '';
65. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
66. var date;
67. if (typeof options.expires == 'number') {
68. date = new Date();
69. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
70. } else {
71. date = options.expires;
72. }
73. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
74. }
75. // NOTE Needed to parenthesize options.path and options.domain
76. // in the following expressions, otherwise they evaluate to undefined
77. // in the packed version for some reason...
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. } else { // only name given, get cookie
83. var cookieValue = null;
84. if (document.cookie && document.cookie != '') {
85. var cookies = document.cookie.split(';');
86. for (var i = 0; i < cookies.length; i++) {
87. var cookie = jQuery.trim(cookies[i]);
88. // Does this cookie string begin with the name we want?
89. if (cookie.substring(0, name.length + 1) == (name + '=')) {
90. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
91. break;
92. }
93. }
94. }
95. return cookieValue;
96. }
97. };