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 相关文章推荐
jQuery 对Select的操作备忘记录
Jul 04 Javascript
纯js网页画板(Graphics)类简介及实现代码
Dec 24 Javascript
设为首页加入收藏兼容360/火狐/谷歌/IE等主流浏览器的代码
Mar 26 Javascript
jQuery源码解读之removeClass()方法分析
Feb 20 Javascript
轻松学习jQuery插件EasyUI EasyUI实现拖动基本操作
Nov 30 Javascript
AngularJS bootstrap启动详解及实例代码
Sep 14 Javascript
js实现自动轮换选项卡
Jan 13 Javascript
JavaScript实现身份证验证代码实例
Aug 26 Javascript
vue 内联样式style中的background用法说明
Aug 05 Javascript
js实现淘宝浏览商品放大镜功能
Oct 28 Javascript
关于angular 8.1使用过程中的一些记录
Nov 25 Javascript
js基于div丝滑实现贝塞尔曲线
Sep 23 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边学边教》(02.Apache+PHP环境配置――上篇)
2006/12/13 PHP
php 验证码制作(网树注释思想)
2009/07/20 PHP
thinkphp在低版本Nginx 下支持PATHINFO的方法分享
2016/05/27 PHP
PHP迭代器和迭代的实现与使用方法分析
2018/04/19 PHP
php让json_encode不自动转义斜杠“/”的方法
2020/04/27 PHP
一个小型js框架myJSFrame附API使用帮助
2008/06/28 Javascript
JavaScript Timer实现代码
2010/02/17 Javascript
Javascript中查找不以XX字符结尾的单词示例代码
2013/10/15 Javascript
JQuery实现绚丽的横向下拉菜单
2013/12/19 Javascript
jquery获得同源iframe内body下标签的值的方法
2014/09/25 Javascript
鼠标经过子元素触发mouseout,mouseover事件的解决方案
2015/07/26 Javascript
js判断日期时间有效性的方法
2015/10/24 Javascript
jQuery自定义滚动条完整实例
2016/01/08 Javascript
浅析JavaScript中break、continue和return的区别
2016/11/30 Javascript
JS实现小米轮播图
2020/09/21 Javascript
Python的装饰器用法学习笔记
2016/06/24 Python
Python调用ctypes使用C函数printf的方法
2017/08/23 Python
一文带你了解Python中的字符串是什么
2018/11/20 Python
python感知机实现代码
2019/01/18 Python
python 实现在tkinter中动态显示label图片的方法
2019/06/13 Python
Win10+GPU版Pytorch1.1安装的安装步骤
2019/09/27 Python
Python遍历字典方式就实例详解
2019/12/28 Python
使用Python实现NBA球员数据查询小程序功能
2020/11/09 Python
HTML5 自动聚焦(autofocus)属性使用介绍
2013/08/07 HTML / CSS
html5在移动端的屏幕适应问题示例探讨
2014/06/15 HTML / CSS
打印机墨盒:123Inkjets
2017/02/16 全球购物
JOSEPH官网:英国奢侈时尚品牌
2018/01/31 全球购物
阿根廷旅游网站:almundo阿根廷
2018/02/12 全球购物
80年代复古T恤:TruffleShuffle
2018/07/02 全球购物
高二历史教学反思
2014/01/25 职场文书
联谊会主持词
2014/03/26 职场文书
设计师求职信模板
2014/05/06 职场文书
2015年幼儿园中班下学期工作总结
2015/05/22 职场文书
红白喜事主持词
2015/07/06 职场文书
开发者首先否认《遗弃》被取消的传言
2022/04/11 其他游戏
vue router 动态路由清除方式
2022/05/25 Vue.js