jquery cookie插件代码类


Posted in Javascript onMay 26, 2009

提供方便方法操作cookie :

$.cookie('the_cookie'); // 获得cookie 
$.cookie('the_cookie', 'the_value'); // 设置cookie 
$.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie 
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'sosuo8.com', secure: true }); 
$.cookie('the_cookie', '', { expires: -1 }); // 删除 
$.cookie('the_cookie', null); // 删除 cookie

代码:
/** 
* Cookie plugin 
* 
* Copyright (c) 2006 Klaus Hartl (stilbuero.de) 
* Dual licensed under the MIT and GPL licenses: 
* http://www.opensource.org/licenses/mit-license.php 
* http://www.gnu.org/licenses/gpl.html 
* 
*/ /** 
* Create a cookie with the given name and value and other optional parameters. 
* 
* @example $.cookie('the_cookie', 'the_value'); 
* @desc Set the value of a cookie. 
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true }); 
* @desc Create a cookie with all available options. 
* @example $.cookie('the_cookie', 'the_value'); 
* @desc Create a session cookie. 
* @example $.cookie('the_cookie', null); 
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain 
* used when the cookie was set. 
* 
* @param String name The name of the cookie. 
* @param String value The value of the cookie. 
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes. 
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. 
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted. 
* If set to null or omitted, the cookie will be a session cookie and will not be retained 
* when the the browser exits. 
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). 
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). 
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will 
* require a secure protocol (like HTTPS). 
* @type undefined 
* 
* @name $.cookie 
* @cat Plugins/Cookie 
* @author Klaus Hartl/klaus.hartl@stilbuero.de 
*/ 
/** 
* Get the value of a cookie with the given name. 
* 
* @example $.cookie('the_cookie'); 
* @desc Get the value of a cookie. 
* 
* @param String name The name of the cookie. 
* @return The value of the cookie. 
* @type String 
* 
* @name $.cookie 
* @cat Plugins/Cookie 
* @author Klaus Hartl/klaus.hartl@stilbuero.de 
*/ 
jQuery.cookie = function(name, value, options) { 
if (typeof value != 'undefined') { // name and value given, set cookie 
options = options || {}; 
if (value === null) { 
value = ''; 
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 
} 
// CAUTION: 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; 
} 
};
Javascript 相关文章推荐
线路分流自动跳转代码;希望对大家有用!
Dec 02 Javascript
打造基于jQuery的高性能TreeView(asp.net)
Feb 23 Javascript
解决jquery的datepicker的本地化以及Today问题
May 23 Javascript
解析offsetHeight,clientHeight,scrollHeight之间的区别
Nov 20 Javascript
JavaScript中实现继承的三种方式和实例
Jan 29 Javascript
JQuery入门基础小实例(1)
Sep 17 Javascript
JS获取数组最大值、最小值及长度的方法
Nov 24 Javascript
遍历js中对象的属性和值的实例
Nov 21 Javascript
AngularJs用户登录问题处理(交互及验证、阻止FQ处理)
Oct 26 Javascript
解决Vue中mounted钩子函数获取节点高度出错问题
May 18 Javascript
jQuery与原生JavaScript选择HTML元素集合用法对比分析
Nov 26 jQuery
vue组件的路由高亮问题解决方法
May 11 Vue.js
判断脚本加载是否完成的方法
May 26 #Javascript
javascript 复杂的嵌套环境中输出单引号和双引号
May 26 #Javascript
Javascript Select操作大集合
May 26 #Javascript
让GoogleCode的SVN下的HTML文件在FireFox下正常显示.
May 25 #Javascript
JavaScript constructor和instanceof,JSOO中的一对欢喜冤家
May 25 #Javascript
jQuery 图像裁剪插件Jcrop的简单使用
May 22 #Javascript
document.compatMode介绍
May 21 #Javascript
You might like
PHP取整数函数常用的四种方法小结
2012/07/05 PHP
PHP set_error_handler()函数使用详解(示例)
2013/11/12 PHP
php删除指定目录的方法
2015/04/03 PHP
PHP Cookie学习笔记
2016/08/23 PHP
Jqgrid表格随窗口大小改变而改变的简单实例
2013/12/28 Javascript
JS代码实现table数据分页效果
2016/05/26 Javascript
原生js封装二级城市下拉列表的实现代码
2016/06/16 Javascript
整理一下常见的IE错误
2016/11/18 Javascript
JS实现复制内容到剪贴板功能
2017/02/05 Javascript
vue + socket.io实现一个简易聊天室示例代码
2017/03/06 Javascript
详解ES6之async+await 同步/异步方案
2017/09/19 Javascript
浅析Angular19 自定义表单控件
2018/01/31 Javascript
[02:39]DOTA2国际邀请赛助威团西雅图第一天
2013/08/08 DOTA
python测试驱动开发实例
2014/10/08 Python
Python正则表达式匹配HTML页面编码
2015/04/08 Python
Python爬虫模拟登录带验证码网站
2016/01/22 Python
windows下python之mysqldb模块安装方法
2017/09/07 Python
Python回文字符串及回文数字判定功能示例
2018/03/20 Python
详解PyTorch批训练及优化器比较
2018/04/28 Python
深入浅析python 中的匿名函数
2018/05/21 Python
python 将md5转为16字节的方法
2018/05/29 Python
对python中词典的values值的修改或新增KEY详解
2019/01/20 Python
利用Python对文件夹下图片数据进行批量改名的代码实例
2019/02/21 Python
使用django实现一个代码发布系统
2019/07/18 Python
使用python实现离散时间傅里叶变换的方法
2019/09/02 Python
python和c语言哪个更适合初学者
2020/06/22 Python
香港通票:Hong Kong Pass
2019/02/26 全球购物
澳大利亚第一旅行车和房车配件店:Caravan RV Camping
2020/12/26 全球购物
运动会入场词200字
2014/02/15 职场文书
2014年党的群众路线教育实践活动总结
2014/04/25 职场文书
激励员工的口号
2014/06/16 职场文书
党员批评与自我批评发言
2014/10/02 职场文书
工作失误检讨书范文
2015/01/26 职场文书
成绩单家长意见
2015/06/03 职场文书
实验室安全管理制度
2015/08/05 职场文书
浅谈Python从全局与局部变量到装饰器的相关知识
2021/06/21 Python