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脚本特性
Sep 13 Javascript
javascript 事件处理、鼠标拖动效果实现方法详解
May 11 Javascript
JavaScript学习笔记记录我的旅程
May 23 Javascript
js中style.display=&quot;&quot;无效的解决方法
Oct 30 Javascript
JS实现支持Ajax验证的表单插件
Mar 24 Javascript
浅谈javascript中的加减时间
Jul 12 Javascript
JavaScript判断浏览器对CSS3属性是否支持的多种方法
Nov 13 Javascript
前端MVVM框架解析之双向绑定
Jan 24 Javascript
angular-tree-component的使用详解
Jul 30 Javascript
element-ui表格列金额显示两位小数的方法
Aug 24 Javascript
微信小程序实现吸顶特效
Jan 08 Javascript
vue-cli3单页构建大型项目方案
Apr 07 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下通过exec获得计算机的唯一标识[CPU,网卡 MAC地址]
2011/06/09 PHP
php switch语句多个值匹配同一代码块的实现
2014/03/03 PHP
yii实现CheckBox复选框在同一行显示的方法
2014/12/03 PHP
yii2整合百度编辑器umeditor及umeditor图片上传问题的解决办法
2016/04/20 PHP
PHP实现文件下载【实例分享】
2017/04/28 PHP
比较全面的event对像在IE与FF中的区别 推荐
2009/09/21 Javascript
js或css实现滚动广告的几种方案
2010/01/28 Javascript
jquery插件 autoComboBox 下拉框
2010/12/22 Javascript
基于jquery的监控数据是否发生改变
2011/04/11 Javascript
浅析JavaScript中的typeof运算符
2013/11/30 Javascript
JavaScript实现刷新不重记的倒计时
2016/08/10 Javascript
jQuery Easyui使用(一)之可折叠面板的布局手风琴菜单
2016/08/17 Javascript
Jquery针对tr td的一些实用操作方法(必看篇)
2016/10/05 Javascript
bootstrap模态框实现拖拽效果
2016/12/14 Javascript
BootStrap表单控件之文本域textarea
2017/05/23 Javascript
Angular.js中上传指令ng-upload的基本使用教程
2017/07/30 Javascript
EasyUI 数据表格datagrid列自适应内容宽度的实现
2019/07/18 Javascript
JS实现的定时器展示简单秒表、页面弹框及跳转操作完整示例
2020/01/26 Javascript
JavaScript实现复选框全选和取消全选
2020/11/20 Javascript
Python自定义装饰器原理与用法实例分析
2018/07/16 Python
Django添加feeds功能的示例
2018/08/07 Python
使用Python画了一棵圣诞树的实例代码
2020/11/27 Python
pandas针对excel处理的实现
2021/01/15 Python
pandas统计重复值次数的方法实现
2021/02/20 Python
Python使用Turtle模块绘制国旗的方法示例
2021/02/28 Python
微软香港官网及网上商店:Microsoft HK
2016/09/01 全球购物
阿联酋团购网站:Groupon阿联酋
2016/10/14 全球购物
微软中国官方旗舰店:销售Surface、Xbox One、笔记本电脑、Office
2018/07/23 全球购物
办公室文秘岗位职责
2013/11/15 职场文书
高中自我评价分享
2013/12/05 职场文书
经典的毕业生自荐信范文
2014/04/14 职场文书
小学庆六一活动总结
2014/08/28 职场文书
博士生专家推荐信
2014/09/26 职场文书
法人代表证明书格式
2014/10/01 职场文书
2014年第四季度入党积极分子思想汇报(十八届四中全会)
2014/11/03 职场文书
业务员辞职信范文
2015/03/02 职场文书