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 最近浏览过的商品的功能实现代码
May 14 Javascript
基于jQuery的动态增删改查表格信息,可左键/右键提示(原创自Zjmainstay)
Jul 31 Javascript
JavaScript自动设置IFrame高度的小例子
Jun 08 Javascript
js模仿windows桌面图标排列算法具体实现(附图)
Jun 16 Javascript
8个超实用的jQuery功能代码分享
Jan 08 Javascript
jQuery+easyui中的combobox实现下拉框特效
Feb 27 Javascript
老生常谈js-react组件生命周期
May 02 Javascript
jQuery实现表格冻结顶栏效果
Aug 20 jQuery
Vue的click事件防抖和节流处理详解
Nov 13 Javascript
解决vue自定义全局消息框组件问题
Nov 22 Javascript
JS实现京东商品分类侧边栏
Dec 11 Javascript
代码解析React中setState同步和异步问题
Jun 03 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
超神学院:天使彦公认最美的三个视角,网友:我的天使快下凡吧!
2020/03/02 国漫
一个简单计数器的源代码
2006/10/09 PHP
Yii视图CGridView列表用法实例分析
2016/07/12 PHP
php将服务端的文件读出来显示在web页面实例
2016/10/31 PHP
js下用eval生成JSON对象
2010/09/17 Javascript
基于Jquery的表格隔行换色,移动换色,点击换色插件
2010/12/22 Javascript
js函数名与form表单元素同名冲突的问题
2014/03/07 Javascript
通过正则表达式获取url中参数的简单实现
2016/06/07 Javascript
JS实现的幻灯片切换显示效果
2016/09/07 Javascript
基于rem的移动端响应式适配方案(详解)
2017/07/07 Javascript
vue2.0使用swiper组件实现轮播效果
2017/11/27 Javascript
基于百度地图api清除指定覆盖物(Overlay)的方法
2018/01/26 Javascript
解决Vue.js 2.0 有时双向绑定img src属性失败的问题
2018/03/14 Javascript
微信小程序中遇到的iOS兼容性问题小结
2018/11/14 Javascript
基于JS实现数字动态变化显示效果附源码
2019/07/18 Javascript
JS事件循环机制event loop宏任务微任务原理解析
2020/08/04 Javascript
IDEA配置jQuery, $符号不再显示黄色波浪线的问题
2020/10/09 jQuery
[00:59]PWL开团时刻DAY7——我在赶
2020/11/06 DOTA
python用户管理系统
2018/03/13 Python
TensorFlow数据输入的方法示例
2018/06/19 Python
python使用__slots__让你的代码更加节省内存
2018/09/05 Python
python小程序基于Jupyter实现天气查询的方法
2020/03/27 Python
浅谈python 类方法/静态方法
2020/09/18 Python
用python-webdriver实现自动填表的示例代码
2021/01/13 Python
python lambda的使用详解
2021/02/26 Python
css3 flex布局 justify-content:space-between 最后一行左对齐
2020/01/02 HTML / CSS
美丽的现代设计家具:2Modern
2018/07/26 全球购物
BASIC HOUSE官方旗舰店:韩国著名的服装品牌
2018/09/27 全球购物
淘宝客服自我总结鉴定
2014/01/25 职场文书
日本语毕业生自荐信
2014/02/01 职场文书
超市总经理岗位职责
2014/02/02 职场文书
农村结婚典礼司仪主持词
2014/03/14 职场文书
园林设计专业毕业生求职信
2014/03/23 职场文书
党员个人剖析材料2014
2014/10/08 职场文书
goland设置颜色和字体的操作
2021/05/05 Golang
HTML5简单实现添加背景音乐的几种方法
2021/05/12 HTML / CSS