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小技巧
Jul 21 Javascript
用示例说明filter()与find()的用法以及children()与find()的区别分析
Apr 26 Javascript
JavaScript二维数组实现的省市联动菜单
May 08 Javascript
JS实现样式清新的横排下拉菜单效果
Oct 09 Javascript
jquery实现百叶窗效果
Jan 12 Javascript
RequireJS 依赖关系的实例(推荐)
Jan 21 Javascript
浅谈Vue-cli 命令行工具分析
Nov 22 Javascript
seajs下require书写约定实例分析
May 16 Javascript
浅谈angular2子组件的事件传递(任意组件事件传递)
Sep 30 Javascript
vue2路由基本用法实例分析
Mar 06 Javascript
单线程JavaScript实现异步过程详解
May 19 Javascript
react项目从新建到部署的实现示例
Feb 19 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中通过虚代理实现延迟加载的实现代码
2011/06/10 PHP
php获取网站根目录物理路径的几种方法(推荐)
2017/03/04 PHP
jquery 插件开发方法小结
2009/10/23 Javascript
通过上下左右键和回车键切换光标实现代码
2013/03/08 Javascript
JavaScript 匿名函数和闭包介绍
2015/04/13 Javascript
简单实现兼容各大浏览器的js复制内容到剪切板
2015/09/09 Javascript
Bootstrap登陆注册页面开发教程
2016/07/12 Javascript
整理关于Bootstrap排版的慕课笔记
2017/03/29 Javascript
详解Windows下安装Nodejs步骤
2017/05/18 NodeJs
Angular 2.0+ 的数据绑定的实现示例
2017/08/09 Javascript
JS设计模式之命令模式概念与用法分析
2018/02/06 Javascript
深入理解与使用keep-alive(配合router-view缓存整个路由页面)
2018/09/25 Javascript
详解vuex中action何时完成以及如何正确调用dispatch的思考
2019/01/21 Javascript
python正则表达式抓取成语网站
2013/11/20 Python
Python实现基于HTTP文件传输实例
2014/11/08 Python
Python的ORM框架中SQLAlchemy库的查询操作的教程
2015/04/25 Python
Python用户推荐系统曼哈顿算法实现完整代码
2017/12/01 Python
基于Python实现用户管理系统
2019/02/26 Python
详解python编译器和解释器的区别
2019/06/24 Python
python GUI库图形界面开发之PyQt5信号与槽事件处理机制详细介绍与实例解析
2020/03/08 Python
Python实现子类调用父类的初始化实例
2020/03/12 Python
Python之Django自动实现html代码(下拉框,数据选择)
2020/03/13 Python
Python使用正则表达式实现爬虫数据抽取
2020/08/17 Python
Html5移动端网页端适配(js+rem)
2021/02/03 HTML / CSS
德国在线香料制造商:Gewürzland
2020/03/10 全球购物
事业单位个人应聘自荐信
2013/09/21 职场文书
光电信息专业应届生求职信
2013/10/07 职场文书
数控专业推荐信范文
2013/12/02 职场文书
市级文明单位申报材料
2014/05/07 职场文书
北京奥运会口号
2014/06/21 职场文书
机械机修工岗位职责
2014/08/03 职场文书
2015年行政执法工作总结
2015/05/23 职场文书
2015年仓库管理工作总结
2015/05/25 职场文书
观看《杨善洲》宣传教育片心得体会
2016/01/23 职场文书
《游戏王:大师决斗》新活动上线 若无符合卡组可免费租用
2022/04/13 其他游戏
Python通用验证码识别OCR库ddddocr的安装使用教程
2022/07/07 Python