可兼容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 相关文章推荐
js prototype 格式化数字 By shawl.qiu
Apr 02 Javascript
jquery创建一个ajax关键词数据搜索实现思路
Feb 26 Javascript
一个检测表单数据的JavaScript实例
Oct 31 Javascript
最实用的jQuery分页插件
Oct 09 Javascript
canvas 绘制圆形时钟
Feb 22 Javascript
微信小程序实现收藏与取消收藏切换图片功能
Aug 03 Javascript
深入Vue-Router路由嵌套理解
Aug 13 Javascript
微信小程序实现的3d轮播图效果示例【基于swiper组件】
Dec 11 Javascript
vue组件文档(.md)中如何自动导入示例(.vue)详解
Jan 25 Javascript
通过实例了解JS 连续赋值
Sep 24 Javascript
jQuery--遍历操作实例小结【后代、同胞及过滤】
May 22 jQuery
js前端设计模式优化50%表单校验代码示例
Jun 21 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
php下网站防IP攻击代码,超级实用
2010/10/24 PHP
php.ini中date.timezone设置分析
2011/07/29 PHP
php4与php5的区别小结(配置异同)
2011/12/20 PHP
php除数取整示例
2014/04/24 PHP
PHP中str_split()函数的用法讲解
2019/04/11 PHP
自写的一个jQuery圆角插件
2010/10/26 Javascript
JavaScript 计算图片加载数量的代码
2011/01/01 Javascript
最新的10款jQuery内容滑块插件分享
2011/09/18 Javascript
jQuery中Dom的基本操作小结
2014/01/23 Javascript
jQuery中empty()方法用法实例
2015/01/16 Javascript
为JS扩展Array.prototype.indexOf引发的问题及解决办法
2015/01/21 Javascript
浅谈js图片前端预览之filereader和window.URL.createObjectURL
2016/06/30 Javascript
JS正则表达式之非捕获分组用法实例分析
2016/12/28 Javascript
JS奇技之利用scroll来监听resize详解
2017/06/15 Javascript
浅谈react-router HashRouter和BrowserRouter的使用
2017/12/29 Javascript
解决layer弹层遮罩挡住窗体的问题
2018/08/17 Javascript
详解vue使用vue-layer-mobile组件实现toast,loading效果
2018/08/31 Javascript
详解webpack打包第三方类库的正确姿势
2018/10/20 Javascript
使用electron制作满屏心特效的示例代码
2018/11/27 Javascript
react+redux仿微信聊天界面
2019/06/21 Javascript
[05:46]2018完美盛典-《同梦共竞》
2018/12/17 DOTA
[35:39]完美世界DOTA2联赛PWL S2 FTD.C vs Rebirth 第二场 11.22
2020/11/24 DOTA
在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
2015/04/09 Python
浅析Python中的多重继承
2015/04/28 Python
基于Python对象引用、可变性和垃圾回收详解
2017/08/21 Python
定制FileField中的上传文件名称实例
2017/08/23 Python
python删除过期log文件操作实例解析
2018/01/31 Python
详解Django-auth-ldap 配置方法
2018/12/10 Python
详解Python_shutil模块
2019/03/15 Python
Python opencv相机标定实现原理及步骤详解
2020/04/09 Python
最新Python idle下载、安装与使用教程图文详解
2020/11/28 Python
css3 box-sizing属性使用参考指南
2013/01/08 HTML / CSS
css3实现背景模糊的三种方式(小结)
2020/05/15 HTML / CSS
校园之星获奖感言
2014/01/29 职场文书
高中语文教材(文学文化常识大全一)
2019/08/13 职场文书
Mysql分析设计表主键为何不用uuid
2022/03/31 MySQL