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 相关文章推荐
JavaScript 给汉字排序实例代码
Jun 28 Javascript
dess中一个简单的多路委托的实现
Jul 20 Javascript
优化innerHTML操作(提高代码执行效率)
Aug 20 Javascript
javascript 闭包
Sep 15 Javascript
JavaScript获取网页中第一个链接ID的方法
Apr 03 Javascript
Jquery实现弹性滑块滑动选择数值插件
Aug 08 Javascript
JS关闭窗口时产生的事件及用法示例
Aug 20 Javascript
js记录点击某个按钮的次数-刷新次数为初始状态的实例
Feb 15 Javascript
微信小程序仿微信运动步数排行(交互)
Jul 13 Javascript
vue设置一开始进入的页面教程
Oct 28 Javascript
JS代码检查工具ESLint介绍与使用方法
Feb 04 Javascript
three.js 实现露珠滴落动画效果的示例代码
Mar 01 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新手用的Insert和Update语句构造类
2012/03/31 PHP
详解PHP内置访问资源的超时时间 time_out file_get_contents read_file
2013/06/03 PHP
Yii使用migrate命令执行sql语句的方法
2016/03/15 PHP
PHP简单实现模拟登陆功能示例
2017/09/15 PHP
php实现推荐功能的简单实例
2019/09/29 PHP
JavaScript 继承详解 第一篇
2009/08/30 Javascript
ExtJS的FieldSet的column列布局
2009/11/20 Javascript
javascript下string.format函数补充
2010/08/24 Javascript
Jquery中显示隐藏的实现代码分析
2011/07/26 Javascript
JS表的模拟方法
2015/02/05 Javascript
JavaScript常用字符串与数组扩展函数小结
2016/04/24 Javascript
学做Bootstrap的第一个页面
2016/05/15 HTML / CSS
jQuery页面加载初始化的3种方法(推荐)
2016/06/02 Javascript
浅谈bootstrap源码分析之scrollspy(滚动侦听)
2016/06/06 Javascript
Vue计算属性的学习笔记
2017/03/22 Javascript
Vue 开发音乐播放器之歌手页右侧快速入口功能
2018/08/08 Javascript
解决IE11 vue +webpack 项目中数据更新后页面没有刷新的问题
2018/09/25 Javascript
vue 中Virtual Dom被创建的方法
2019/04/15 Javascript
JavaScript使用面向对象实现的拖拽功能详解
2019/06/12 Javascript
[01:01:43]EG vs VP 2018国际邀请赛淘汰赛BO3 第二场 8.24
2018/08/25 DOTA
Python中的并发编程实例
2014/07/07 Python
python使用点操作符访问字典(dict)数据的方法
2015/03/16 Python
Python实现对字符串的加密解密方法示例
2017/04/29 Python
用python制作游戏外挂
2018/01/04 Python
python删除服务器文件代码示例
2018/02/09 Python
对python中Matplotlib的坐标轴的坐标区间的设定实例讲解
2018/05/25 Python
pycharm远程linux开发和调试代码的方法
2018/07/17 Python
DataFrame:通过SparkSql将scala类转为DataFrame的方法
2019/01/29 Python
使用Python创建简单的HTTP服务器的方法步骤
2019/04/26 Python
H5调用相机拍照并压缩图片的实例代码
2017/07/20 HTML / CSS
彼得罗夫美国官网:Peter Thomas Roth美国(青瓜面膜)
2017/11/05 全球购物
学生学习总结的自我评价
2013/10/22 职场文书
英文求职信结束语大全
2013/10/26 职场文书
2014高中生入党思想汇报范文
2014/09/13 职场文书
golang中的空接口使用详解
2021/03/30 Python
MySQL中EXPLAIN语句及用法
2022/05/20 MySQL