可兼容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浮点数乘积运算出现多位小数的解决方法
Feb 17 Javascript
jQuery中size()方法用法实例
Dec 27 Javascript
js制作简易年历完整实例
Jan 28 Javascript
理解javascript对象继承
Apr 17 Javascript
JS只能输入正整数的简单实例
Oct 07 Javascript
vue 请求后台数据的实例代码
Jun 22 Javascript
zTree 树插件实现全国五级地区点击后加载的示例
Feb 05 Javascript
在vue中使用jointjs的方法
Mar 24 Javascript
JS数组实现分类统计实例代码
Sep 30 Javascript
JS字符串和数组如何实现相互转化
Jul 02 Javascript
解决vue项目获取dom元素宽高总是不准确问题
Jul 29 Javascript
Vue select 绑定动态变量的实例讲解
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
MySQL数据源表结构图示
2008/06/05 PHP
php 模拟POST|GET操作实现代码
2010/07/20 PHP
php whois查询API制作方法
2011/06/23 PHP
php中拷贝构造函数、赋值运算符重载
2012/07/25 PHP
探讨如何使用SimpleXML函数来加载和解析XML文档
2013/06/07 PHP
PHP动态生成指定大小随机图片的方法
2016/03/25 PHP
thinkPHP框架对接支付宝即时到账接口回调操作示例
2016/11/14 PHP
PHP策略模式定义与用法示例
2017/07/27 PHP
简短几句jquery代码的实现一个图片向上滚动切换
2011/09/02 Javascript
jQuery 插件封装的方法
2016/11/16 Javascript
js实现省份下拉菜单效果
2017/02/15 Javascript
Vue中computed与methods的区别详解
2018/03/24 Javascript
Angular网络请求的封装方法
2018/05/22 Javascript
vue debug 二种方法
2018/09/16 Javascript
微信小程序分享海报生成的实现方法
2018/12/10 Javascript
使用js实现一个简单的滚动条过程解析
2019/09/10 Javascript
[38:42]完美世界DOTA2联赛循环赛 Matador vs Forest BO2第二场 11.05
2020/11/05 DOTA
[01:01:01]完美世界DOTA2联赛循环赛 GXR vs FTD BO2第一场 10.29
2020/10/29 DOTA
使用Python读写文本文件及编写简单的文本编辑器
2016/03/11 Python
python用装饰器自动注册Tornado路由详解
2017/02/14 Python
Python cookbook(数据结构与算法)实现优先级队列的方法示例
2018/02/18 Python
python仿evething的文件搜索器实例代码
2019/05/13 Python
java判断三位数的实例讲解
2019/06/10 Python
python 动态迁移solr数据过程解析
2019/09/04 Python
Vs Code中8个好用的python 扩展插件
2020/10/12 Python
详解Selenium-webdriver绕开反爬虫机制的4种方法
2020/10/28 Python
python读写数据读写csv文件(pandas用法)
2020/12/14 Python
使用html5 canvas创建太空游戏的示例
2014/05/08 HTML / CSS
巴西购物网站:Submarino
2020/01/19 全球购物
怎样比较两个类型为String的字符串
2016/08/17 面试题
会计师事务所审计实习自我鉴定
2013/09/20 职场文书
党建工作目标管理责任书
2015/01/29 职场文书
机器人总动员观后感
2015/06/09 职场文书
学术会议领导致辞
2015/07/29 职场文书
丧事答谢词大全
2015/09/30 职场文书
python 如何在 Matplotlib 中绘制垂直线
2021/04/02 Python