Jquery中文网 www.jquerycn.cn
Jquery中文网 >  jQuery  >  jquery 教程  >  正文 jquery.cookie用法详细解析

jquery.cookie用法详细解析

发布时间:2014-05-28   编辑:www.jquerycn.cn
本篇文章主要是对jquery.cookie的用法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助

一个轻量级的cookie插件,可以读取、写入、删除cookie。

jquery.cookie.js的配置

首先包含jQuery的库文件,在后面包含jquery.cookie.js的库文件

<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>


使用方法

新添加一个会话cookie:


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

注:当没有指明cookie有效时间时,所创建的cookie有效期默认到用户关闭浏览器为止,所以被称为“会话cookie(session cookie)”
 

创建一个cookie并设置有效时间为7天:


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

注:当指明了cookie有效时间时,所创建的cookie被称为“持久cookie(persistent cookie)”。


创建一个cookie并设置cookie的有效路径:

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

注:在默认情况下,只有设置cookie的网页才能读取该cookie。如果想让一个页面读取另一个页面设置的cookie,必须设置cookie的路径。

cookie的路径用于设置能够读取cookie的顶级目录。将这个路径设置为网站的根目录,可以让所有网页都能互相读取cookie(一般不要这样设置,防止出现冲突)


读取cookie:

$.cookie('the_cookie');

// cookie存在 => 'the_value' $.cookie('not_existing'); // cookie不存在 => null


删除cookie,通过传递null作为cookie的值即可:

$.cookie('the_cookie', null);


相关参数的解释

expires: 365

定义cookie的有效时间,值可以是一个(从创建cookie时算起,以天为单位)或一个Date。

如果省略,那么创建的cookie是会话cookie,将在用户退出浏览器时被删除。
 

path: '/'

默认情况:只有设置cookie的网页才能读取该cookie。

定义cookie的有效路径。默认情况下,该参数的值为创建cookie的网页所在路径(标准浏览器的行为)。

如果你想在整个网站中访问这个cookie需要这样设置有效路径:path: '/'。

如果你想删除一个定义了有效路径的cookie,你需要在调用函数时包含这个路径:$.cookie('the_cookie', null, { path: '/' });。


domain: 'example.com'

默认值:创建cookie的网页所拥有的域名。
 

secure: true

默认值:false。如果为true,cookie的传输需要使用安全协议(HTTPS)。
 

raw: true

默认值:false。 默认情况下,读取和写入cookie的时候自动进行编码和解码(使用encodeURIComponent编码,decodeURIComponent解码)。

要关闭这个功能设置raw: true即可。


$.cookie('the_cookie'); // get cookie $.cookie('the_cookie', 'the_value'); // set cookie $.cookie('the_cookie', 'the_value', { expires: 7 }); // set cookie with an expiration date seven days in the future $.cookie('the_cookie', '', { expires: -1 }); // delete cookie
$.cookie('the_cookie', null); // delete cookie


$.cookie('the_cookie','the_value', {expires: 7, path: '/', domain:'80tvb.com', secure: true});//完整调用方式

//或者这样:$.cookie('the_cookie','the_value');

//删除Cookie: $.cookie('the_cookie',null);

 

jQuery操作cookie的插件,大概的使用方法如下

$.cookie('the_cookie'); //读取Cookie值
$.cookie('the_cookie', ‘the_value'); //设置cookie的值
$.cookie('the_cookie', ‘the_value', {expires: 7, path: ‘/', domain: ‘jquery.com', secure: true});//新建一个cookie 包括有效期 路径域名等
$.cookie('the_cookie', ‘the_value'); //新建cookie
$.cookie('the_cookie', null); //删除一个cookie


jquery设置cookie过期时间与检查cookies是否可用

让cookies在x分钟后过期
var date = new date();
date.settime(date.gettime() + (x * 60 * 1000));
$.cookie(‘example', ‘foo', { expires: date });

$.cookie(‘example', ‘foo', { expires: 7});


检查cookies是否可用
$(document).ready(function() {var dt = new date();dt.setseconds(dt.getseconds() + 60);document.cookie = “cookietest=1; expires=” + dt.togmtstring();var cookiesenabled = document.cookie.indexof(“cookietest=”) != -1;if(!cookiesenabled){//cookies不能用……..}}); 

您可能感兴趣的文章:
jquery.cookie用法详细解析
Illustrator绘制宝石方法解析分享
Illustrator做的字体变弯曲方法详解
Illustrator制作WIN8图标飞开碎片效果教程
photoshop里布尔运算中经常出现问题解决方法
photoshop布尔运算中经常出现问题解决方法分享
Illustrator通过实例解析图像上色经验技巧分享
php date_parse函数的使用方法详解
微插件推荐系列:jquery.cookie
Illustrator颜色面板使用经验技巧分享

关键词: jquery  cookie   
[关闭]