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里使用Dom操作Xml
Jan 22 Javascript
jquery easyui滚动条部分设置介绍
Sep 12 Javascript
javascript去除空格方法小结
May 21 Javascript
js限制文本框的输入内容代码分享(3类)
Aug 20 Javascript
Js查找字符串中出现次数最多的字符及个数实例解析
Sep 05 Javascript
微信小程序利用canvas 绘制幸运大转盘功能
Jul 06 Javascript
Vue登录注册并保持登录状态的方法
Aug 17 Javascript
vue 刷新之后 嵌套路由不变 重新渲染页面的方法
Sep 13 Javascript
JS数组求和的常用方法实例小结
Jan 07 Javascript
JS使用Prim算法和Kruskal算法实现最小生成树
Jan 17 Javascript
JavaScript计算出两个数的差值
Mar 19 Javascript
javascript设计模式 ? 观察者模式原理与用法实例分析
Apr 22 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
第十四节 命名空间 [14]
2006/10/09 PHP
一个odbc连mssql分页的类
2006/10/09 PHP
使用PHP curl模拟浏览器抓取网站信息
2013/10/28 PHP
php对数组内元素进行随机调换的方法
2015/05/12 PHP
PHP编写RESTful接口的方法
2016/02/21 PHP
php 解决扫描二维码下载跳转问题
2017/01/13 PHP
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
2019/11/25 PHP
JavaScript Sort 的一个错误用法示例
2015/03/20 Javascript
JS实现点击网页判断是否安装app并打开否则跳转app store
2016/11/18 Javascript
详解微信小程序 template添加绑定事件
2017/06/23 Javascript
vue.js的手脚架vue-cli项目搭建的步骤
2017/08/30 Javascript
JS二分查找算法详解
2017/11/01 Javascript
vue2.0的虚拟DOM渲染思路分析
2018/08/09 Javascript
JointJS流程图的绘制方法
2018/12/03 Javascript
Vue源码解析之数组变异的实现
2018/12/04 Javascript
Python 代码性能优化技巧分享
2012/08/07 Python
Python collections模块实例讲解
2014/04/07 Python
解决python3 urllib中urlopen报错的问题
2017/03/25 Python
解决tensorflow模型参数保存和加载的问题
2018/07/26 Python
Tensorflow 自定义loss的情况下初始化部分变量方式
2020/01/06 Python
HTML5在手机端实现视频全屏展示方法
2020/11/23 HTML / CSS
意大利奢华内衣制造商:Cosabella
2017/08/29 全球购物
美国便宜的横幅和标志印刷在线:Best of Signs
2019/05/29 全球购物
旅游管理本科生求职信
2013/10/14 职场文书
受欢迎的大学生自我评价
2013/12/05 职场文书
销售心得体会
2014/01/02 职场文书
致接力运动员广播稿
2014/02/17 职场文书
教师考核材料
2014/05/21 职场文书
美术课外活动总结
2014/07/08 职场文书
小学生春游活动方案
2014/08/20 职场文书
2014年残联工作总结
2014/11/21 职场文书
教师党员自我评价2015
2015/03/04 职场文书
美德少年主要事迹材料
2015/11/04 职场文书
zabbix agent2 监控oracle数据库的方法
2021/05/13 Oracle
Python selenium绕过webdriver监测执行javascript
2022/04/12 Python
Python如何快速找到多个字典中的公共键(key)
2022/04/29 Python