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 cookies 设置、读取、删除实例代码
Apr 12 Javascript
js图片延迟加载的实现方法及思路
Jul 22 Javascript
jquery实现div拖拽宽度示例代码
Jul 31 Javascript
JS实现多物体缓冲运动实例代码
Nov 29 Javascript
解决jQuery uploadify在非IE核心浏览器下无法上传
Aug 05 Javascript
详解JavaScript中Hash Map映射结构的实现
May 21 Javascript
基于JSON格式数据的简单jQuery幻灯片插件(jquery-slider)
Aug 10 Javascript
JavaScript使用Range调色及透明度实例
Sep 25 Javascript
Angularjs 设置全局变量的方法总结
Oct 20 Javascript
javascript实现右下角广告框效果
Feb 01 Javascript
解决vue router组件状态刷新消失的问题
Aug 01 Javascript
JS实现滑动拼图验证功能完整示例
Mar 29 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源码之explode使用说明
2011/08/05 PHP
解析CI即CodeIgniter框架在Nginx下的重写规则
2013/06/03 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十五)
2014/06/30 PHP
php实现获取及设置用户访问页面语言类
2014/09/24 PHP
PHP INT类型在内存中占字节详解
2019/07/20 PHP
让任务管理器中的CPU跳舞的js代码
2008/11/01 Javascript
基于jquery自己写tab滑动门(通用版)
2012/10/30 Javascript
用jquery方法操作radio使其默认选项是否
2013/09/10 Javascript
javascript中的document.open()方法使用介绍
2013/10/09 Javascript
jquery ajax post提交数据乱码
2013/11/05 Javascript
js实现日期级联效果
2014/01/23 Javascript
node.js调用C++开发的模块实例
2015/07/03 Javascript
Sublime Text 3常用插件及安装方法
2015/12/16 Javascript
Nodejs中session的简单使用及通过session实现身份验证的方法
2016/02/04 NodeJs
输入法的回车与消息发送快捷键回车的冲突解决方法
2016/08/09 Javascript
jQuery EasyUI封装简化操作
2016/09/18 Javascript
AngularJS模板加载用法详解
2016/11/04 Javascript
BootStrap实现响应式布局导航栏折叠隐藏效果(在小屏幕、手机屏幕浏览时自动折叠隐藏)
2016/11/30 Javascript
JavaScript实现弹出广告功能
2017/03/30 Javascript
Angular中$state.go页面跳转并传递参数的方法
2017/05/09 Javascript
jQuery接受后台传递的List的实例详解
2017/08/02 jQuery
基于Vue实现支持按周切换的日历
2020/09/24 Javascript
详解React开发必不可少的eslint配置
2018/02/05 Javascript
JavaScript实现移动端带transition动画的轮播效果
2020/03/24 Javascript
微信小程序实现上拉加载功能示例【加载更多数据/触底加载/点击加载更多数据】
2020/05/29 Javascript
vue.js 输入框输入值自动过滤特殊字符替换中问标点操作
2020/08/31 Javascript
[02:01]大师之路——DOTA2完美大师赛11月论剑上海
2017/11/06 DOTA
零基础写python爬虫之抓取百度贴吧代码分享
2014/11/06 Python
python中urllib模块用法实例详解
2014/11/19 Python
Python文件及目录操作实例详解
2015/06/04 Python
python使用pyqt写带界面工具的示例代码
2017/10/23 Python
Python第三方Window模块文件的几种安装方法
2018/11/22 Python
详解pandas绘制矩阵散点图(scatter_matrix)的方法
2020/04/23 Python
会计岗位职责
2013/11/08 职场文书
优秀毕业生求职信
2014/06/05 职场文书
Python利用机器学习算法实现垃圾邮件的识别
2021/06/28 Python