jquery.cookie
A simple, lightweight jQuery plugin for reading, writing and deleting cookies.
一个轻量级读、写和删除cookies的JQuery 插件
Build Status Matrix
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为空,则为当前域名
相关推荐
cookie设置插件jquery.cookie.min.js 文章《javascript设置cookie高级篇可跨域访问》https://blog.csdn.net/cplvfx/article/details/117822956
jquery cookie.js 官方下载,一款优秀的 jquery 插件,提供了非常轻量级、简单、实用的操作 cookie 的方法,包括读写、删除等操作,jquery cookie 路径,jquery cookie 时间,jquery cookie 有效期,jquery cookie ...
jquery.cookie.js 提供了jquery中非常简单的操作cookie的方法。 $.cookie('the_cookie'); // 获得cookie $.cookie('the_cookie', 'the_value'); // 设置cookie $.cookie('the_cookie', 'the_value', { expires: 7 })...
jQuery cookie是个很好的cookie插件,大概的使用方法如下 example $.cookie(’name’, ‘value’); 设置cookie的值,把name变量的值设为value example $.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, ...
一天弹一次jquery.cookie插件是一款简单的jquery.cookie插件使弹窗点击关闭后一天弹一次。
使用cookies将我们最近选择的一个主题保存、方便我们下一次登录时看到的还是我们上次选择的主题。
jQuery.cookie.js插件实现换肤功能.docx
简单的jquery.cookie插件使弹窗点击关闭后一天弹一次
jquery-1.4.2.js jquery-1.4.2.min.js jquery-1.4.2-vsdoc.js 中文版vsdoc jquery-1.4.2-vsdoc_en.js jquery.cookie.js
引入jQuery与jQuery.Cookie.js插件,通过 $.cookie获得cookie对象
Jquery全屏遮掩及加载条插件,使用: <script type="text/javascript" src="js/...<script type="text/javascript" src="js/jquery.cookie.js"> <script type="text/javascript" src="js/jquery.blockUI.js"></script>
简单的jquery.cookie插件使弹窗点击关闭后一天弹一次 简单的jquery.cookie插件使弹窗点击关闭后一天弹一次 简单的jquery.cookie插件使弹窗点击关闭后一天弹一次
里面包含了3个js文件jQuery-1.6.2.min.js、jquery.colorbox.js、jquery.cookie.js。 其中jquery.cookie.js 是js处理cookie用到得很方便 jquery.colorbox.js在处理图片显示时挺好用的jquery插件
jquery.cookie.js插件工具,已使用在项目中。分享资源大家一起共享
本文实例讲述了jQuery结合jQuery.cookie.js插件实现换肤功能。分享给大家供大家参考,具体如下: 上一次和大家分享了如何实现换肤功能,但是script代码好像有点长,所以这次打算使用cookie.js插件来实现换肤功能,...