可兼容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 相关文章推荐
jquery BS,dialog控件自适应大小
Jul 06 Javascript
Extjs Gird 支持中文拼音排序实现代码
Apr 15 Javascript
node.js中的http.response.writeHead方法使用说明
Dec 14 Javascript
浅谈javascript中this在事件中的应用
Feb 15 Javascript
JavaScript匿名函数之模仿块级作用域
Dec 12 Javascript
jQuery实现iframe父窗体和子窗体的相互调用
Jun 17 Javascript
window.close(); 关闭浏览器窗口js代码的总结介绍
Jul 14 Javascript
web 屏蔽BackSpace键实例代码
Dec 24 Javascript
JS+canvas动态绘制饼图的方法示例
Sep 12 Javascript
javascript高仿热血传奇游戏实现代码
Feb 22 Javascript
layer弹出子iframe层父子页面传值的实现方法
Nov 22 Javascript
JS实现简单的九宫格抽奖
Jun 28 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
phpfans留言版用到的数据操作类和分页类
2007/01/04 PHP
Thinkphp5框架ajax接口实现方法分析
2019/08/28 PHP
jQuery实现table隔行换色和鼠标经过变色的两种方法
2014/06/15 Javascript
js实现两点之间画线的方法
2015/05/12 Javascript
JS截取与分割字符串常用技巧总结
2015/11/10 Javascript
轻松使用jQuery双向select控件Bootstrap Dual Listbox
2015/12/13 Javascript
使用bootstrap实现多窗口和拖动效果
2016/09/22 Javascript
Javascript 数组去重的方法(四种)详解及实例代码
2016/11/24 Javascript
AngularJS控制器controller给模型数据赋初始值的方法
2017/01/04 Javascript
easyui 中的datagrid跨页勾选问题的实现方法
2017/01/18 Javascript
react-router4 嵌套路由的使用方法
2017/07/24 Javascript
JS实现按钮控制计时开始和停止功能
2017/07/27 Javascript
详解Angular结合zTree异步加载节点数据
2018/01/20 Javascript
vue(2.x,3.0)配置跨域代理
2019/11/27 Javascript
微信小程序按顺序同步执行的两种方式
2019/12/20 Javascript
微信小程序实现日历签到
2020/09/21 Javascript
初步探究Python程序的执行原理
2015/04/11 Python
JavaScript中的模拟事件和自定义事件实例分析
2018/07/27 Python
python 输出所有大小写字母的方法
2019/01/02 Python
python中while和for的区别总结
2019/06/28 Python
python中 * 的用法详解
2019/07/10 Python
HTML5 canvas 瀑布流文字效果的示例代码
2018/01/31 HTML / CSS
Schecker荷兰:狗狗用品和配件
2019/06/06 全球购物
俄罗斯EPL钻石珠宝店:ЭПЛ
2019/10/22 全球购物
银行柜员应聘推荐信范文
2013/11/24 职场文书
门卫岗位安全职责
2013/12/13 职场文书
敬老文明号事迹材料
2014/01/16 职场文书
单位工程竣工验收方案
2014/03/16 职场文书
沙滩主题婚礼活动策划方案
2014/09/15 职场文书
毕业证代领委托书
2014/09/26 职场文书
办护照工作证明
2014/10/01 职场文书
2015年招聘工作总结
2014/12/12 职场文书
保研专家推荐信范文
2015/03/25 职场文书
培训讲师开场白
2015/06/01 职场文书
导游词之新疆尼雅遗址
2019/10/16 职场文书
python如何做代码性能分析
2021/04/26 Python