学习论坛 会员投稿 RSS订阅 站内通告:
搜索: 您的位置网站源代码 > 网页制作 > ajax教程 > jquery插件操作Cookie的插件使用方法

jquery插件操作Cookie的插件使用方法

2010-01-01 11:07:39 来源:原创 【 】 浏览:
摘要: jquery插件之jQuery操作Cookie的插件使用方法 - JavaScript

有了jquery操作Cookie就变得简单多了,不使用纯JS写一大堆代码,下面来看下Jquery操作Cookie插件的源码:

 JS Code [http://www.isstudy.com]
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; } };

看完源码后,我们来看下这个插件的使用方法:

 Code [http://www.isstudy.com]
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

赶快试下吧。


顶一下
(1)
100%
踩一下
(0)
0%
Tags: ajax教程 cookie插件
责任编辑:小人物
收藏】 【挑错】 【推荐】 【打印
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
关于我们 | 网站声明 | 广告服务 | 招聘英才 | 联系我们 | 网站地图 | RSS订阅 |