成都知之科技有限公司

知之互动专注成都\南充\乐山\绵阳\贵阳\兰州等地的品牌网站建设,是西南最具专业实力的网站建设网络公司。给成都众多知名企业提供过高品质的网站建设、移动手机网站设计、门户社区与电子商务B2C网站与运营、微信应用与医疗网站建设、百度推广托管(竞价、网盟帐户托管)与运营及营销推广等服务,赠送域名空间邮箱,做网站请致电18200120015.

jQuery操作Cookie插件

更新:2015-03-20 15:02来源:成都知之科技点击:

本文将介绍jQuery如何操作Cookie插件,需要了解的朋友可以参考下

代码如下:

jQuery.cookie = function(name, value, options) { 
if (typeof value != 'undefined') { // name and value given, set cookie 
options = options || {}; 
if (value === null) { 
value = ''; 
options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed 
options.expires = -1; 
} 
var expires = ''; 
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { 
var date; 
if (typeof options.expires == 'number') { 
date = new Date(); 
date.setTime(date.getTime() (options.expires * 24 * 60 * 60 * 1000)); 
} else { 
date = options.expires; 
} 
expires = '; expires=' date.toUTCString(); // use expires attribute, max-age is not supported by IE 
} 
// NOTE Needed to parenthesize options.path and options.domain 
// in the following expressions, otherwise they evaluate to undefined 
// in the packed version for some reason... 
var path = options.path ? '; path=' (options.path) : ''; 
var domain = options.domain ? '; domain=' (options.domain) : ''; 
var secure = options.secure ? '; secure' : ''; 
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); 
} else { // only name given, get cookie 
var cookieValue = null; 
if (document.cookie && document.cookie != '') { 
var cookies = document.cookie.split(';'); 
for (var i = 0; i < cookies.length; i ) { 
var cookie = jQuery.trim(cookies[i]); 
// Does this cookie string begin with the name we want? 
if (cookie.substring(0, name.length 1) == (name '=')) { 
cookieValue = decodeURIComponent(cookie.substring(name.length 1)); 
break; 
} 
} 
} 
return cookieValue; 
} 
};

然后看了下Discuz!中对cookie的操作方法
如下,发现少了个遍历用;分割的数组的处理
function getcookie(name) { var cookie_start = document.cookie.indexOf(name); var cookie_end = document.cookie.indexOf(";", cookie_start); return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length))); } function setcookie(cookieName, cookieValue, seconds, path, domain, secure) { var expires = new Date(); expires.setTime(expires.getTime() + seconds); document.cookie = escape(cookieName) + '=' + escape(cookieValue) + (expires ? '; expires=' + expires.toGMTString() : '') + (path ? '; path=' + path : '/') + (domain ? '; domain=' + domain : '') + (secure ? '; secure' : ''); }

使用方法 

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 

设置一个名称为blog,值为css9.net的cookie: 

$.cookie("blog", "css9.net"); 

设置一个名称为blog,值为css9.net的cookie,同时设置过期时间(expires属性)为7天: 

$.cookie("blog", "css9.net", { expires: 7 }); 

设置一个名称为blog,值为css9.net的cookie,设置过期时间(expires属性)为7天,同时设置cookie的path属性为&rdquo;/admin&rdquo; 

$.cookie("blog", "css9.net", { path: '/admin', expires: 7 }); 

读取Cookie: 

读取名称为blog的cookie值: 

alert( $.cookie("blog") ); 

删除cookie: 

$.cookie("example", null);

 

jQuery ztabs选项卡插件zhiz.net
使用jquery.qrcode生成二维码
© 2014 www.zhiz.net All Rights Reserved. 知之科技 蜀ICP备11006209号