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 相关文章推荐
使用Json比用string返回数据更友好,也更面向对象一些
Sep 13 Javascript
关于eval 与new Function 到底该选哪个?
Apr 17 Javascript
JavaScript中的prototype和constructor简明总结
Apr 05 Javascript
跟我学习javascript的prototype,getPrototypeOf和__proto__
Nov 17 Javascript
深入理解jQuery之防止冒泡事件
May 24 Javascript
【经典源码收藏】基于jQuery的项目常见函数封装集合
Jun 07 Javascript
jQuery插件echarts实现的多柱子柱状图效果示例【附demo源码下载】
Mar 04 Javascript
详解jQuery同步Ajax带来的UI线程阻塞问题及解决办法
Aug 09 jQuery
详解如何在微信小程序开发中正确的使用vant ui组件
Sep 13 Javascript
AngularJs返回前一页面时刷新一次前面页面的方法
Oct 09 Javascript
解决layui中onchange失效以及form动态渲染失效的问题
Sep 27 Javascript
javascript实现京东登录显示隐藏密码
Aug 02 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
apache2.2.4+mysql5.0.77+php5.2.8安装精简
2009/04/29 PHP
PHP中单引号与双引号的区别分析
2014/08/19 PHP
PHP编程中的Session阻塞问题与解决方法分析
2017/08/07 PHP
PHP实现函数内修改外部变量值的方法示例
2018/12/28 PHP
给网站上的广告“加速”显示的方法
2007/04/08 Javascript
js 弹出新页面避免被浏览器、ad拦截的一种新方法
2014/04/30 Javascript
JQuery中$(document)是什么意思有什么作用
2014/07/21 Javascript
采用自执行的匿名函数解决for循环使用闭包的问题
2014/09/11 Javascript
推荐5 个常用的JavaScript调试技巧
2015/01/08 Javascript
jQuery插件MixItUp实现动画过滤和排序
2015/04/12 Javascript
浅谈javascript中replace()方法
2015/11/10 Javascript
Seajs是什么及sea.js 由来,特点以及优势
2016/10/13 Javascript
js 获取本地文件及目录的方法(推荐)
2016/11/10 Javascript
微信小程序 欢迎界面开发的实例详解
2016/11/30 Javascript
JS基于onclick事件实现单个按钮的编辑与保存功能示例
2017/02/13 Javascript
详解nodejs中exports和module.exports的区别
2017/02/17 NodeJs
详解angular如何调用HTML字符串的方法
2018/06/30 Javascript
详解在Node.js中发起HTTP请求的5种方法
2019/01/10 Javascript
详解VUE单页应用骨架屏方案
2019/01/17 Javascript
通过JQuery,JQueryUI和Jsplumb实现拖拽模块
2019/06/18 jQuery
JS使用H5实现图片预览功能
2019/09/30 Javascript
vue中用 async/await 来处理异步操作
2020/07/18 Javascript
vue中配置scss全局变量的步骤
2020/12/28 Vue.js
[04:10]2016国际邀请赛中国区预选赛第二日TOP10精彩集锦
2016/06/28 DOTA
Python字符串中查找子串小技巧
2015/04/10 Python
python数据类型判断type与isinstance的区别实例解析
2017/10/31 Python
tensorflow之并行读入数据详解
2020/02/05 Python
HTML5 canvas实现雪花飘落特效
2016/03/08 HTML / CSS
Marriott国际:万豪国际酒店查询预订
2017/09/25 全球购物
时尚孕妇装:Ingrid & Isabel
2019/05/08 全球购物
铭宣海淘转运:美国、日本、英国转运等全球转运公司
2019/09/10 全球购物
为什么要使用servlet
2016/01/17 面试题
秋季运动会通讯稿
2014/01/24 职场文书
一份没有按时交货失信于客户的检讨书
2014/09/19 职场文书
专题民主生活会对照检查材料思想汇报
2014/09/29 职场文书
2014年管理人员工作总结
2014/12/01 职场文书