可兼容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不是基础的基础
Dec 24 Javascript
JS修改css样式style浅谈
May 06 Javascript
js为数字添加逗号并格式化数字的代码
Aug 23 Javascript
jquery放大镜效果超漂亮噢
Nov 15 Javascript
原生js FileReader对象实现图片上传本地预览效果
Mar 27 Javascript
简单实现vue验证码60秒倒计时功能
Oct 11 Javascript
jQuery实现点击下拉框中的值累加到文本框中的方法示例
Oct 28 jQuery
jQuery+CSS实现的标签页效果示例【测试可用】
Aug 14 jQuery
微信小程序自定义键盘 内部虚拟支付
Dec 20 Javascript
vue实现分页组件
Jun 16 Javascript
vue+element实现表格新增、编辑、删除功能
May 28 Javascript
解决vue安装less报错Failed to compile with 1 errors的问题
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
一个很方便的 XML 类!!原创的噢
2006/10/09 PHP
有道搜索和IP138的IP的API接口(PHP应用)
2012/11/29 PHP
phpexcel导出excel的颜色和网页中的颜色显示不一致
2012/12/11 PHP
php使用PDO事务配合表格读取大量数据插入操作实现方法
2017/02/16 PHP
ThinkPHP3.1.x修改成功与失败跳转页面的方法
2017/09/29 PHP
Laravel中validation验证 返回中文提示 全局设置的方法
2019/09/29 PHP
XML文件转化成NSData对象的方法
2015/08/12 Javascript
JSON+Jquery省市区三级联动
2016/01/13 Javascript
DOM操作和jQuery实现选项移动操作的简单实例
2016/06/07 Javascript
BootStrap Progressbar 实现大文件上传的进度条的实例代码
2016/06/27 Javascript
浅谈jQuery中hide和fadeOut的区别 show和fadeIn的区别
2016/08/18 Javascript
js实现图片360度旋转
2017/01/22 Javascript
微信小程序中hidden不生效原因的解决办法
2017/04/26 Javascript
vue子组件使用自定义事件向父组件传递数据
2017/05/27 Javascript
深究AngularJS中$sce的使用
2017/06/12 Javascript
三分钟学会用ES7中的Async/Await进行异步编程
2018/06/14 Javascript
聊聊Vue 中 title 的动态修改问题
2019/06/11 Javascript
前端插件之Bootstrap Dual Listbox使用教程
2019/07/23 Javascript
js实现开关灯效果
2020/03/30 Javascript
vue 根据选择条件显示指定参数的例子
2019/11/09 Javascript
[36:29]2018DOTA2亚洲邀请赛 4.1 小组赛 A组加赛 LGD vs TNC
2018/04/02 DOTA
python3+PyQt5 使用三种不同的简便项窗口部件显示数据的方法
2019/06/17 Python
python基于selenium爬取斗鱼弹幕
2021/02/20 Python
解决HTML5手机端页面缩放的问题
2017/10/27 HTML / CSS
商务英语大学生职业生涯规划书范文
2014/01/01 职场文书
求职信范文英文版
2014/01/05 职场文书
美术专业自荐信
2014/07/07 职场文书
庆国庆活动总结
2014/08/28 职场文书
企业挂职心得体会
2014/09/10 职场文书
仲裁协议书
2014/09/26 职场文书
查摆问题整改措施
2014/10/24 职场文书
收银员岗位职责
2015/02/03 职场文书
六一亲子活动感想
2015/08/07 职场文书
pycharm debug 断点调试心得分享
2021/04/16 Python
python实现三次密码验证的示例
2021/04/29 Python
Java实现学生管理系统(IO版)
2022/02/24 Java/Android