可兼容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 相关文章推荐
google地图的路线实现代码
Aug 20 Javascript
JavaScript传递变量: 值传递?引用传递?
Feb 22 Javascript
javascript使用isNaN()函数判断变量是否为数字
Sep 21 Javascript
关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器
Feb 11 Javascript
js style动态设置table高度
Oct 21 Javascript
Vue.js基础知识汇总
Apr 27 Javascript
JavaScript中windows.open()、windows.close()方法详解
Jul 28 Javascript
Gulp实现静态网页模块化的方法详解
Jan 09 Javascript
使用Angular CLI从蓝本生成代码详解
Mar 24 Javascript
JavaScript DOM元素常见操作详解【添加、删除、修改等】
May 09 Javascript
Angular5集成eventbus的示例代码
Jul 19 Javascript
继承行为在 ES5 与 ES6 中的区别详解
Dec 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中利用wsdl创建标准webservice的实现代码
2011/12/07 PHP
用PHP和Shell写Hadoop的MapReduce程序
2014/04/15 PHP
PHP使用PDO创建MySQL数据库、表及插入多条数据操作示例
2019/05/30 PHP
一文看懂PHP进程管理器php-fpm
2020/06/01 PHP
JS写的数字拼图小游戏代码[学习参考]
2008/10/29 Javascript
asp.net下利用js实现返回上一页的实现方法小集
2009/11/24 Javascript
JavaScript 学习笔记(六)
2009/12/31 Javascript
javascript中使用正则计算中文长度的例子
2014/04/29 Javascript
node.js中的Socket.IO使用实例
2014/11/04 Javascript
jQuery调用WebMethod(PageMethod) NET2.0的方法
2016/04/15 Javascript
javascript实现用户点击数量统计
2016/12/25 Javascript
基于JavaScript实现弹幕特效
2020/08/27 Javascript
使用DataTable插件实现异步加载数据
2017/11/19 Javascript
Vue.js下拉菜单组件使用方法详解
2019/10/19 Javascript
如何构建 vue-ssr 项目的方法步骤
2020/08/04 Javascript
[02:05]2014DOTA2国际邀请赛 BBC外卡赛赛后总结
2014/07/09 DOTA
[01:20]PWL开团时刻DAY9——听说潮汐没用?
2020/11/10 DOTA
Python实现简易Web爬虫详解
2018/01/03 Python
django的auth认证,authenticate和装饰器功能详解
2019/07/25 Python
Django 使用easy_thumbnails压缩上传的图片方法
2019/07/26 Python
详解使用python3.7配置开发钉钉群自定义机器人(2020年新版攻略)
2020/04/01 Python
解决Python中报错TypeError: must be str, not bytes问题
2020/04/07 Python
Selenium alert 弹窗处理的示例代码
2020/08/06 Python
基于python实现简单C/S模式代码实例
2020/09/14 Python
Python timeit模块原理及使用方法
2020/10/10 Python
python在地图上画比例的实例详解
2020/11/13 Python
HTML5移动端开发遇见的东西
2019/10/11 HTML / CSS
ONLY瑞典官网:世界知名服装品牌
2018/06/19 全球购物
自1926年以来就为冰岛保持温暖:66°North
2020/11/27 全球购物
毕业生就业推荐信范文
2013/12/01 职场文书
高三历史教学反思
2014/01/09 职场文书
运动会入场解说词300字
2014/01/25 职场文书
幼儿园家长评语
2014/02/10 职场文书
欢迎新生标语2015
2015/07/16 职场文书
2015年国庆节新闻稿
2015/07/18 职场文书
Java并发编程必备之Future机制
2021/06/30 Java/Android