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 相关文章推荐
查看图片(前进后退)功能实现js代码
Apr 24 Javascript
JavaScript生成随机数的4种自定义函数分享
Feb 28 Javascript
jQuery增加自定义函数的方法
Jul 18 Javascript
简单实现jquery焦点图
Dec 12 Javascript
js+html制作简单验证码
Feb 16 Javascript
无循环 JavaScript(map、reduce、filter和find)
Apr 08 Javascript
原生JS获取元素的位置与尺寸实现方法
Oct 18 Javascript
微信小程序实现写入读取缓存详解
Aug 30 Javascript
JS对象属性的检测与获取操作实例分析
Mar 17 Javascript
javascript+css实现进度条效果
Mar 25 Javascript
javascript实现支付宝滑块验证码效果
Jul 24 Javascript
JavaScript获取时区实现过程解析
Sep 24 Javascript
判断脚本加载是否完成的方法
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启用zlib压缩文件的配置方法
2013/06/12 PHP
PHP 如何利用phpexcel导入数据库
2013/08/24 PHP
php设置静态内容缓存时间的方法
2014/12/01 PHP
Zend Framework入门应用实例详解
2016/12/11 PHP
PHP数组操作实例分析【添加,删除,计算,反转,排序,查找等】
2016/12/24 PHP
Nigma vs Alliance BO5 第二场2.14
2021/03/10 DOTA
论坛里点击别人帖子下面的回复,回复标题变成“回复 24# 的帖子”
2009/06/14 Javascript
jquery异步请求实例代码
2011/06/21 Javascript
捕获键盘事件(且兼容各浏览器)
2013/07/03 Javascript
让alert不出现弹窗的两种方法
2014/05/18 Javascript
在Linux上用forever实现Node.js项目自启动
2014/07/09 Javascript
jQuery中append()方法用法实例
2014/12/25 Javascript
基于jQuey实现鼠标滑过变色(整行变色)
2015/12/07 Javascript
jQuery 翻页组件yunm.pager.js实现div局部刷新的思路
2016/08/11 Javascript
浅谈JS函数定义方式的区别
2016/10/30 Javascript
深究AngularJS中ng-drag、ng-drop的用法
2017/06/12 Javascript
Vue2.0 给Tab标签页和页面切换过渡添加样式的方法
2018/03/13 Javascript
详解vuex结合localstorage动态监听storage的变化
2018/05/03 Javascript
浅谈webpack SplitChunksPlugin实用指南
2018/09/17 Javascript
过滤器vue.filters的使用方法实现
2019/09/18 Javascript
微信公众号H5之微信分享常见错误和问题(小结)
2019/11/14 Javascript
angular异步验证防抖踩坑实录
2019/12/01 Javascript
vue组件内部引入外部js文件的方法
2020/01/18 Javascript
Vue单文件组件开发实现过程详解
2020/07/30 Javascript
vue3.0+vue-router+element-plus初实践
2020/12/02 Vue.js
介绍Python的@property装饰器的用法
2015/04/28 Python
python线程、进程和协程详解
2016/07/19 Python
Sanic框架路由用法实例分析
2018/07/16 Python
Python3 + Appium + 安卓模拟器实现APP自动化测试并生成测试报告
2021/01/27 Python
pandas统计重复值次数的方法实现
2021/02/20 Python
CSS3实现时间轴效果
2016/07/11 HTML / CSS
密封类可以有虚函数吗
2014/08/11 面试题
电子信息工程专业推荐信
2014/02/14 职场文书
售后客服工作职责
2014/06/16 职场文书
韩语专业职业生涯规划范文:成功之路就在我们脚下
2014/09/11 职场文书
2015年教师学期工作总结
2015/04/30 职场文书