jQuery API 返回首页目录 | jQuery API 中英文对照版
$.cookie(name, value, options)

$.cookie(name, value, options)

Create a cookie with the given name and value and other optional parameters.

返回值

undefined

参数

  • name (String): The name of the cookie.
  • value (String): The value of the cookie.
  • options (Object): An object literal containing key/value pairs to provide optional cookie attributes.

Hash 选项

  • expires (Number|Date): Either an integer specifying the expiration date from now on in days or a Date object. If a negative value is specified (e.g. a date in the past), the cookie will be deleted. If set to null or omitted, the cookie will be a session cookie and will not be retained when the the browser exits.
  • path (String): The value of the path atribute of the cookie (default: path of page that created the cookie).
  • domain (String): The value of the domain attribute of the cookie (default: domain of page that created the cookie).
  • secure (Boolean): If true, the secure attribute of the cookie will be set and the cookie transmission will require a secure protocol (like HTTPS).

示例

说明:

Set the value of a cookie.

jQuery 代码:
$.cookie('the_cookie', 'the_value');
说明:

Create a cookie with all available options.

jQuery 代码:
$.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
说明:

Create a session cookie.

jQuery 代码:
$.cookie('the_cookie', 'the_value');
说明:

Delete a cookie by setting a date in the past.

jQuery 代码:
$.cookie('the_cookie', '', {expires: -1});
相关链接
asp之家 | jQuery官方网站 | jQuery中文网 | 电子书作者网站 | 电子书作者blog