可兼容IE的获取及设置cookie的jquery.cookie函数方法


Posted in Javascript onSeptember 02, 2013

前言

在开发过程中,因为之前有接触过Discuz,就直接拿其common.js里面的getcookie和setcookie方法来使用,做到后面在使用IE来测试的时候,发现这两个方法子啊IE下不起作用,就请教同事,这样就有了jquery.cookie.js文件的由来,里面的代码很少,我贴在下面,方便以后使用和研究吧。

源码

/** 
* 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. 
* 
* @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 
} 
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 函数式编程
Aug 16 Javascript
jQuery仿Excel表格编辑功能的实现代码
May 01 Javascript
详解JavaScript中双等号引起的隐性类型转换
May 30 Javascript
关于bootstrap日期转化,bootstrap-editable的简单使用,bootstrap-fileinput的使用详解
May 12 Javascript
Vue编写多地区选择组件
Aug 21 Javascript
浅谈mint-ui loadmore组件注意的问题
Nov 08 Javascript
Vue实现active点击切换方法
Mar 16 Javascript
详解Vue开发微信H5微信分享签名失败问题解决方案
Aug 09 Javascript
利用JS实现一个同Excel表现的智能填充算法
Aug 13 Javascript
关于JSON解析的实现过程解析
Oct 08 Javascript
微信小程序 自定义弹窗实现过程(附代码)
Dec 05 Javascript
关于对TypeScript泛型参数的默认值理解
Jul 15 Javascript
基于MVC3方式实现下拉列表联动(JQuery)
Sep 02 #Javascript
javascript模块化是什么及其优缺点介绍
Sep 02 #Javascript
火狐下table中创建form导致两个table之间出现空白
Sep 02 #Javascript
js的alert弹出框出现乱码解决方案
Sep 02 #Javascript
javascript中的window.location.search方法简介
Sep 02 #Javascript
js Math 对象的方法
Sep 01 #Javascript
javascript ready和load事件的区别示例介绍
Aug 30 #Javascript
You might like
THINKPHP支持YAML配置文件的设置方法
2015/03/17 PHP
php中二维数组排序问题方法详解
2015/08/28 PHP
php基于openssl的rsa加密解密示例
2016/07/11 PHP
ThinkPHP5.0 图片上传生成缩略图实例代码说明
2018/06/20 PHP
js TextArea的选中区域处理
2010/12/28 Javascript
JS异常处理的一个想法(sofish)
2013/03/14 Javascript
js生成动态表格并为每个单元格添加单击事件的方法
2014/04/14 Javascript
浅谈Jquery为元素绑定事件
2015/04/27 Javascript
javascript HTML+CSS实现经典橙色导航菜单
2016/02/16 Javascript
网页挂马方式整理及详细介绍
2016/11/03 Javascript
js获取当前时间(昨天、今天、明天)
2016/11/23 Javascript
基于JavaScript实现瀑布流效果
2017/03/29 Javascript
ES6入门教程之let和const命令详解
2017/05/17 Javascript
vue中将网页打印成pdf实例代码
2017/06/15 Javascript
AngularJS select设置默认值的实现方法
2017/08/25 Javascript
小程序点击图片实现自动播放视频
2020/05/29 Javascript
如何检查一个对象是否为空
2019/04/11 Javascript
layui时间控件选择时间范围的实现方法
2019/09/28 Javascript
微信小程序新闻网站详情页实例代码
2020/01/10 Javascript
Javascript表单序列化原理及实现代码详解
2020/10/30 Javascript
Python选择排序、冒泡排序、合并排序代码实例
2015/04/10 Python
从Python的源码浅要剖析Python的内存管理
2015/04/16 Python
使用Python写个小监控
2016/01/27 Python
Numpy中stack(),hstack(),vstack()函数用法介绍及实例
2018/01/09 Python
Python3 Click模块的使用方法详解
2020/02/12 Python
python通过文本在一个图中画多条线的实例
2020/02/21 Python
浅析CSS3中鲜为人知的属性:-webkit-tap-highlight-color
2017/01/12 HTML / CSS
HTML5中drawImage用法分析
2014/12/01 HTML / CSS
优衣库美国官网:UNIQLO美国
2018/04/14 全球购物
文秘专业大学生求职信
2013/11/10 职场文书
本科毕业生求职自荐信
2014/02/03 职场文书
教师现实表现材料
2014/02/14 职场文书
环境科学专业教师求职信
2014/07/12 职场文书
少先大队干部竞选稿
2015/11/20 职场文书
2016圣诞节贺卡寄语
2015/12/07 职场文书
基于Pygame实现简单的贪吃蛇游戏
2021/12/06 Python