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判断样式className同时增加class或删除class
Jan 30 Javascript
JavaScript 32位整型无符号操作示例
Dec 08 Javascript
jquery实现的网页自动播放声音
Apr 30 Javascript
跟我学习javascript的this关键字
May 28 Javascript
Knockoutjs 学习系列(一)ko初体验
Jun 07 Javascript
Ext JS动态加载JavaScript创建窗体的方法
Jun 23 Javascript
IE下JS保存图片的简单实例
Jul 15 Javascript
微信开发之调起摄像头、本地展示图片、上传下载图片实例
Dec 08 Javascript
Vue兼容ie9的问题全面解决方案
Jun 19 Javascript
vue-router+nginx 非根路径配置方法
Jun 30 Javascript
javascript实现导航栏分页效果
Jun 27 Javascript
微信小程序实现蒙版弹出窗功能
Sep 17 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
使用VisualStudio开发php的图文设置方法
2010/08/21 PHP
Zend Guard使用指南及问题处理
2015/01/07 PHP
ThinkPHP框架安全实现分析
2016/03/14 PHP
PHP7安装Redis扩展教程【Linux与Windows平台】
2016/09/30 PHP
PHP的简单跳转提示的实现详解
2019/03/14 PHP
laravel框架路由分组,中间件,命名空间,子域名,路由前缀实例分析
2020/02/18 PHP
javascript 面向对象编程基础:封装
2009/08/21 Javascript
你必须知道的Javascript知识点之&quot;this指针&quot;的应用
2013/04/23 Javascript
jQuery 事件的命名空间简单了解
2013/11/22 Javascript
开发中可能会用到的jQuery小技巧
2014/03/07 Javascript
利用jQuery设计一个简单的web音乐播放器的实例分享
2016/03/08 Javascript
简单实现jQuery轮播效果
2017/08/18 jQuery
canvas+gif.js打造自己的数字雨头像的示例代码
2017/10/26 Javascript
Vue中使用ElementUI使用第三方图标库iconfont的示例
2018/10/11 Javascript
ES6知识点整理之对象解构赋值应用示例
2019/04/17 Javascript
怎么使用javascript深度拷贝一个数组
2019/06/06 Javascript
vue 实现模糊检索并根据其他字符的首字母顺序排列
2019/09/19 Javascript
vue实现文件上传读取及下载功能
2020/11/17 Javascript
Vue生命周期activated之返回上一页不重新请求数据操作
2020/07/26 Javascript
openlayers4.6.5实现距离量测和面积量测
2020/09/25 Javascript
[47:52]DOTA2-DPC中国联赛正赛 iG vs LBZS BO3 第二场 3月4日
2021/03/11 DOTA
介绍Python中几个常用的类方法
2015/04/08 Python
django之常用命令详解
2016/06/30 Python
Python中pip更新和三方插件安装说明
2018/07/08 Python
Python实现TCP探测目标服务路由轨迹的原理与方法详解
2019/09/04 Python
Python多线程及其基本使用方法实例分析
2019/10/29 Python
详解pandas获取Dataframe元素值的几种方法
2020/06/14 Python
python 6行代码制作月历生成器
2020/09/18 Python
美国领先的家庭智能音响系统品牌:Sonos
2018/07/20 全球购物
库房主管岗位职责
2013/12/31 职场文书
爱情保证书大全
2014/04/29 职场文书
核心价值观演讲稿
2014/05/13 职场文书
建筑专业毕业生自荐信
2014/05/25 职场文书
家具商场的活动方案
2014/08/16 职场文书
个人对照检查材料思想汇报(四风问题)
2014/09/25 职场文书
Tensorflow与RNN、双向LSTM等的踩坑记录及解决
2021/05/31 Python