jQuery cookie使用

A simple, lightweight jQuery plugin for reading, writing and deleting cookies.

Usage

创建会话cookie:

$.cookie('name', 'value');

创建一个过期时间为7天的cookie:

$.cookie('name', 'value', { expires: 7 });

创建一个有效期的cookie,并设置它的存储路径:

$.cookie('name', 'value', { expires: 7, path: '/' });

如果不写path的话,默认会跟创建页面的路径一样.

读取cookie:

$.cookie('name'); // => "value"
$.cookie('nothing'); // => undefined

读取所有有效的cookie:

$.cookie(); // => { "name": "value" }

删除cookie:

cookie删除成功返回true,否则返回false:

$.removeCookie('name'); // => true
$.removeCookie('nothing'); // => false

删除一个带有路径的cookie:

$.cookie('name', 'value', { path: '/' });

// This won't work!
$.removeCookie('name'); // => false

// This will work!
$.removeCookie('name', { path: '/' }); // => true

Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.

Configuration

关闭cookie编码
raw

By default the cookie value is encoded/decoded when writing/reading, using encodeURIComponent/decodeURIComponent. Bypass this by setting raw to true:

$.cookie.raw = true;

json格式自动转换
json

Turn on automatic storage of JSON objects passed as the cookie value. Assumes JSON.stringify and JSON.parse:

$.cookie.json = true;

Cookie Options

Cookie attributes can be set globally by setting properties of the $.cookie.defaults object or individually for each call to $.cookie() by passing a plain object to the options argument. Per-call options override the default options.
过期时间
expires

expires: 365

Define lifetime of the cookie. Value can be a Number which will be interpreted as days from time of creation or a Date object. If omitted, the cookie becomes a session cookie.
cookie存储路径路径
path

path: '/'

Define the path where the cookie is valid. By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior). If you want to make it available for instance across the entire domain use path: '/'. Default: path of page where the cookie was created.

不能这样创建
This means one cannot set a path using path: window.location.pathname in case such pathname contains a filename like so: /check.html (or at least, such cookie cannot be read correctly).

cookie可用的域名
domain

domain: 'example.com'

Define the domain where the cookie is valid. Default: domain of page where the cookie was created.

secure:true cookie需要https
secure

secure: true

If true, the cookie transmission requires a secure protocol (https). Default: false.

Converters

没有找到相应的应用场景
Provide a conversion function as optional last argument for reading, in order to change the cookie's value to a different representation on the fly.

Example for parsing a value into a number:

$.cookie('foo', '42');
$.cookie('foo', Number); // => 42
Dealing with cookies that have been encoded using escape (3rd party cookies):

$.cookie.raw = true;
$.cookie('foo', unescape);
You can pass an arbitrary conversion function.

原文地址:https://www.cnblogs.com/wspblog/p/6254181.html