可兼容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 相关文章推荐
Display SQL Server Version Information
Jun 21 Javascript
jQuery取得设置清空select选择的文本与值
Jul 08 Javascript
jquery简单实现图片切换效果的方法
May 12 Javascript
全面理解JavaScript中的继承(必看)
Jun 16 Javascript
pc加载更多功能和移动端下拉刷新加载数据
Nov 07 Javascript
js实现登录验证码
Dec 22 Javascript
最常用的jQuery表单验证(简单)
May 23 jQuery
vue 标签属性数据绑定和拼接的实现方法
May 17 Javascript
js遍历添加栏目类添加css 再点击其它删除css【推荐】
Jun 12 Javascript
基于vue、react实现倒计时效果
Aug 26 Javascript
vue打开子组件弹窗都刷新功能的实现
Sep 21 Javascript
Node.js文本文件BOM头的去除方法
Nov 22 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
wordpress之wp-settings.php
2007/08/17 PHP
浅谈php优化需要注意的地方
2014/11/27 PHP
PHP获取当前时间不准确问题解决方案
2020/08/14 PHP
JS中 用户登录系统的解决办法
2013/04/15 Javascript
javascript实现checkbox全选的代码
2015/04/30 Javascript
招聘网站基于jQuery实现自动刷新简历
2015/05/10 Javascript
JavaScript中string对象
2015/06/12 Javascript
Laydate时间组件在火狐浏览器下有多时间输入框时只能给第一个输入框赋值的解决方法
2016/08/18 Javascript
将JSON字符串转换成Map对象的方法
2016/11/30 Javascript
表格展示利器 Bootstrap Table实例代码
2017/09/06 Javascript
JavaScript实现区块链
2018/03/14 Javascript
CentOS7中源码编译安装NodeJS的完整步骤
2018/10/13 NodeJs
taro开发微信小程序的实践
2019/05/21 Javascript
JavaScript之数组扁平化详解
2019/06/03 Javascript
基于webpack4+vue-cli3项目实现换肤功能
2019/07/17 Javascript
解决vue打包后刷新页面报错:Unexpected token
2019/08/27 Javascript
antd-mobile ListView长列表的数据更新遇到的坑
2020/04/08 Javascript
Vue的props父传子的示例代码
2020/05/20 Javascript
vuex 多模块时 模块内部的mutation和action的调用方式
2020/07/24 Javascript
[06:15]2016国际邀请赛中国区预选赛单车采访:我顶WINGS
2016/06/27 DOTA
python中实现将多个print输出合成一个数组
2018/04/19 Python
如何用Python绘制3D柱形图
2020/09/16 Python
H5调用相机拍照并压缩图片的实例代码
2017/07/20 HTML / CSS
canvas进阶之贝塞尔公式推导与物体跟随复杂曲线的轨迹运动
2018/01/10 HTML / CSS
英国护肤品购物网站:Beauty Expert
2016/08/19 全球购物
经典优秀个人求职自荐信格式
2013/09/25 职场文书
幼儿园毕业教师感言
2014/02/21 职场文书
中职生求职信
2014/07/01 职场文书
2014国庆节演讲稿:祖国在我心中(400字)
2014/09/25 职场文书
乡镇三严三实学习心得体会
2014/10/13 职场文书
小学信息技术教学反思
2016/02/16 职场文书
浅谈Python魔法方法
2021/06/28 Java/Android
Java集成swagger文档组件
2021/06/28 Java/Android
使用CSS连接数据库的方式
2022/02/28 HTML / CSS
Redis中key的过期删除策略和内存淘汰机制
2022/04/12 Redis
python数字图像处理:图像的绘制
2022/06/28 Python