`
wenjinglian
  • 浏览: 808291 次
  • 性别: Icon_minigender_1
  • 来自: 株洲->深圳
社区版块
存档分类
最新评论

jquery.cookie.js插件

阅读更多

jquery.cookie  

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

一个轻量级读、写和删除cookies的JQuery 插件

Build Status Matrix

Selenium Test Status

Installation

Include script after the jQuery library (unless you are packaging scripts somehow else):

<script src="/path/to/jquery.cookie.js"></script>

Do not include the script directly from GitHub (http://raw.github.com/...). The file is being served as text/plain and as such being blocked in Internet Explorer on Windows 7 for instance (because of the wrong MIME type). Bottom line: GitHub is not a CDN.

请不要直接引用github.com jquery.cookie.js文件,github 不是CDN

The plugin can also be loaded as AMD module.

插件也支持AMD 模块加载.     AMD百科

Usage

Create session cookie:

$.cookie('the_cookie', 'the_value');

Create expiring cookie, 7 days from then:

创建cookie,7天过期

$.cookie('the_cookie', 'the_value', { expires: 7 });

Create expiring cookie, valid across entire site:

创建过期cookie,在整个站点有效

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

Read cookie:

$.cookie('the_cookie'); // => "the_value"
$.cookie('not_existing'); // => undefined

Read all available cookies:

$.cookie(); // => { "the_cookie": "the_value", "...remaining": "cookies" }

Delete cookie:

// Returns true when cookie was found, false when no cookie was found...
//当cookie值找到返回true,没找到返回false

$.removeCookie('the_cookie');

// Same path as when the cookie was written...
// 同样的路径cookie会被重写
$.removeCookie('the_cookie', { path: '/' });

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.

备注:当删除一个cookie时,必须使用设置cookie时的完全一样的路径,域名和安全选项,否则你就只能依靠默认选项删除

 

Configuration

raw

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

cookie值是否经过处理

$.cookie.raw = true;

json

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

开启自行存存储为JSON对象,使用 JSON.stringify 和 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的生命周期. 值可以是个数字类型也可以是Date对象,如果值为数将会被直接当做天数来创建. 如果不填,则cookie则变成会话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.

Note regarding Internet Explorer:

Due to an obscure bug in the underlying WinINET InternetGetCookie implementation, IE’s document.cookie will not return a cookie if it was set with a path attribute containing a filename.

(From Internet Explorer Cookie Internals (FAQ))

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).

domain

domain: 'example.com'

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

定义cookie域名作用域,默认值为页面的当前域名

secure

secure: true

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

如果为true,则必须是https协议 默认: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.

 

补充:

Path默认值为”/”,表示该Cookie在整个站点内有效。除定义的Cookie要在全站作用页面使用的情况外,其他定义的Cookie要指定其Path属性,遵循作用域最小原则,如:定义Cookie的Path值为”/Shoes”,表示该Cookie在Shoes目录下的所有页面会向服务回复该Cookie信息

domain为空,则为当前域名

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics