可兼容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 相关文章推荐
AJAX 网页保留浏览器前进后退等功能
Feb 12 Javascript
各浏览器中querySelector和querySelectorAll的实现差异分析
May 23 Javascript
JS 页面计时器示例代码
Oct 28 Javascript
JavaScript脚本判断蜘蛛来源的方法
Sep 22 Javascript
使用pcs api往免费的百度网盘上传下载文件的方法
Mar 17 Javascript
利用angular、react和vue实现相同的面试题组件
Feb 19 Javascript
微信小程序实现签字功能
Dec 23 Javascript
浅谈Vue组件单元测试究竟测试什么
Feb 05 Javascript
微信小程序实现限制用户转发功能的实例代码
Feb 22 Javascript
如何实现js拖拽效果及原理解析
May 08 Javascript
解决qrcode.js生成二维码时必须定义一个空div的问题
Jul 09 Javascript
vue实现简单加法计算器
Oct 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
晶体管单管来复再生式收音机
2021/03/02 无线电
PHP中cookies使用指南
2007/03/16 PHP
php中替换字符串中的空格为逗号','的方法
2014/06/09 PHP
PHP使用NuSOAP调用Web服务的方法
2015/07/18 PHP
PHP实现仿Google分页效果的分页函数
2015/07/29 PHP
PHP+MySQL之Insert Into数据插入用法分析
2015/09/27 PHP
jquery 图片上传按比例预览插件集合
2011/05/28 Javascript
jQuery EasyUI API 中文文档 - Spinner微调器使用
2011/10/21 Javascript
捕获浏览器关闭、刷新事件不同情况下的处理方法
2013/06/02 Javascript
js中typeof的用法汇总
2013/12/12 Javascript
JQuery中DOM事件冒泡实例分析
2015/06/13 Javascript
js实现的早期滑动门菜单效果代码
2015/08/27 Javascript
JS实现设置ff与ie元素绝对位置的方法
2016/03/08 Javascript
jquery实现ajax加载超时提示的方法
2016/07/23 Javascript
详谈JS中数组的迭代方法和归并方法
2017/08/11 Javascript
带你快速理解javascript中的事件模型
2017/08/14 Javascript
手机注册发送验证码倒计时的简单实例
2017/11/15 Javascript
vue项目打包之后背景样式丢失的解决方案
2019/01/17 Javascript
[08:07]DOTA2每周TOP10 精彩击杀集锦vol.8
2014/06/25 DOTA
MySQLdb ImportError: libmysqlclient.so.18解决方法
2014/08/21 Python
python解析xml文件实例分析
2015/05/27 Python
Python金融数据可视化汇总
2017/11/17 Python
在python中使用requests 模拟浏览器发送请求数据的方法
2018/12/26 Python
Django实现web端tailf日志文件功能及实例详解
2019/07/28 Python
python 表格打印代码实例解析
2019/10/12 Python
python将字符串转变成dict格式的实现
2019/11/18 Python
Python爬虫爬取电影票房数据及图表展示操作示例
2020/03/27 Python
利用django创建一个简易的博客网站的示例
2020/09/29 Python
python中的split、rsplit、splitlines用法说明
2020/10/23 Python
物流业务员岗位职责
2014/02/08 职场文书
四群教育工作实施方案
2014/03/26 职场文书
园艺师求职信
2014/04/27 职场文书
三八妇女节寄语
2015/02/27 职场文书
小英雄雨来观后感
2015/06/09 职场文书
防震减灾主题班会
2015/08/14 职场文书
Python进程池与进程锁之语法学习
2022/04/11 Python