可兼容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 放大镜 移动镜片效果代码
May 09 Javascript
jQuery的animate函数学习记录
Aug 08 Javascript
使用postMesssage()实现跨域iframe页面间的信息传递方法
Mar 29 Javascript
Javascript中return的使用与闭包详解
Jan 11 Javascript
微信小程序 scroll-view隐藏滚动条详解
Jan 16 Javascript
JavaScript原生节点操作小结
Jan 17 Javascript
JavaScript表单验证实现代码
May 22 Javascript
Angular中点击li标签实现更改颜色的核心代码
Dec 08 Javascript
JavaScript实现的超简单计算器功能示例
Dec 23 Javascript
简单理解Vue中的nextTick方法
Jan 30 Javascript
Vue之Vue.set动态新增对象属性方法
Feb 23 Javascript
Vue封装的组件全局注册并引用
Jul 24 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判断远程图片是否存在的几种方法
2014/05/04 PHP
详解PHP导入导出CSV文件
2014/11/03 PHP
PHP中返回引用类型的方法
2015/04/03 PHP
PHP脚本监控Nginx 502错误并自动重启php-fpm
2015/05/13 PHP
PHP读取文本文件并逐行输出该行使用最多的字符与对应次数的方法
2016/11/25 PHP
jquery 实现的全选和反选
2009/04/15 Javascript
JQUERY 对象与DOM对象之两者相互间的转换
2009/04/27 Javascript
javascript模拟的Ping效果代码 (Web Ping)
2011/03/13 Javascript
js实现目录链接,内容跟着目录滚动显示的简单实例
2016/10/15 Javascript
微信小程序 实现拖拽事件监听实例详解
2016/11/16 Javascript
jQuery和CSS仿京东仿淘宝列表导航菜单
2017/01/04 Javascript
vue.js选中动态绑定的radio的指定项
2017/06/02 Javascript
vue.js如何更改默认端口号8080为指定端口的方法
2017/07/14 Javascript
解决Angular4项目部署到服务器上刷新404的问题
2018/08/31 Javascript
小程序server请求微信服务器超时的解决方法
2019/05/21 Javascript
Node Express用法详解【安装、使用、路由、中间件、模板引擎等】
2020/05/13 Javascript
简单总结Python中序列与字典的相同和不同之处
2016/01/19 Python
Python中单、双下划线的区别总结
2017/12/01 Python
django文档学习之applications使用详解
2018/01/29 Python
浅谈Python用QQ邮箱发送邮件时授权码的问题
2018/01/29 Python
python实现电子产品商店
2019/02/26 Python
python调用摄像头拍摄数据集
2019/06/01 Python
如何使用Python自动控制windows桌面
2019/07/11 Python
用python写测试数据文件过程解析
2019/09/25 Python
Python实现发票自动校核微信机器人的方法
2020/05/22 Python
在python下实现word2vec词向量训练与加载实例
2020/06/09 Python
Python3中小括号()、中括号[]、花括号{}的区别详解
2020/11/15 Python
python3爬虫中多线程的优势总结
2020/11/24 Python
Html5 webRTC简单实现视频调用的示例代码
2020/09/23 HTML / CSS
工程资料员岗位职责
2014/03/10 职场文书
三字经教学反思
2014/04/26 职场文书
护林防火标语
2014/06/27 职场文书
党员对十八届四中全会的期盼思想汇报范文
2014/10/17 职场文书
师德培训心得体会2016
2016/01/09 职场文书
《包身工》教学反思
2016/02/23 职场文书
基于python制作简易版学生信息管理系统
2021/04/20 Python