可兼容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 相关文章推荐
filters.revealTrans.Transition使用方法小结
Aug 19 Javascript
关于javascript function对象那些迷惑分析
Oct 24 Javascript
使用JavaScript脚本无法直接改变Asp.net中Checkbox控件的Enable属性的解决方法
Sep 16 Javascript
浅析JavaScript Array和string的转换(推荐)
May 20 Javascript
javascript实现任务栏消息提示的简单实例
May 31 Javascript
javascript 小数乘法结果错误的处理方法
Jul 28 Javascript
原生JS查找元素的方法(推荐)
Nov 22 Javascript
JS操作时间 - UNIX时间戳的简单介绍(必看篇)
Aug 16 Javascript
js拖动滑块和点击水波纹效果实例代码
Oct 16 Javascript
解决使用layui的时候form表单中的select等不能渲染的问题
Sep 18 Javascript
vue 使用外部JS与调用原生API操作示例
Dec 02 Javascript
vue3.0封装轮播图组件的步骤
Mar 04 Vue.js
基于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
判“新”函数:得到今天与明天的秒数
2006/10/09 PHP
一些使用频率比较高的php函数
2008/10/03 PHP
MySql 按时间段查询数据方法(实例说明)
2008/11/02 PHP
php从右向左/从左向右截取字符串的实现方法
2011/11/28 PHP
JavaScript与Div对层定位和移动获得坐标的实现代码
2010/09/08 Javascript
jquery和javascript的区别(常用方法比较)
2013/07/04 Javascript
Eclipse去除js(JavaScript)验证错误
2014/02/11 Javascript
JavaScript利用正则表达式去除日期中的-
2014/06/09 Javascript
两种方法基于jQuery实现IE浏览器兼容placeholder效果
2014/10/14 Javascript
ECMAScript 5中的属性描述符详解
2015/03/02 Javascript
JavaScript移除数组内重复元素的方法
2015/03/18 Javascript
JQuery中两个ul标签的li互相移动实现方法
2015/05/18 Javascript
jQuery插件制作之全局函数用法实例
2015/06/01 Javascript
javascript拖拽应用实例(二)
2016/03/25 Javascript
JavaScript lodash常见用法系列小结
2016/08/24 Javascript
JavaScript正则表达式校验与递归函数实际应用实例解析
2017/08/04 Javascript
Vue中render方法的使用详解
2018/01/26 Javascript
python链接Oracle数据库的方法
2015/06/28 Python
python 提取tuple类型值中json格式的key值方法
2018/12/31 Python
python使用selenium实现批量文件下载
2019/03/11 Python
PyQt5的PyQtGraph实践系列3之实时数据更新绘制图形
2019/05/13 Python
解决webdriver.Chrome()报错:Message:'chromedriver' executable needs to be in Path
2019/06/12 Python
python实现爬虫抓取小说功能示例【抓取金庸小说】
2019/08/09 Python
redis数据库及与python交互用法简单示例
2019/11/01 Python
推荐值得学习的12款python-web开发框架
2020/08/10 Python
HTML4和HTML5之间除了相似以外的10个主要不同
2012/12/13 HTML / CSS
什么是.net
2015/08/03 面试题
学校火灾防控方案
2014/06/09 职场文书
娱乐节目策划方案
2014/06/10 职场文书
幼儿园感恩节活动方案
2014/10/06 职场文书
大学生毕业个人总结
2015/02/15 职场文书
安全伴我行主题班会
2015/08/13 职场文书
小学体育跳绳课教学反思
2016/02/16 职场文书
聊聊Python中关于a=[[]]*3的反思
2021/06/02 Python
「天才王子的赤字国家重生术」妮妮姆·拉雷粘土人开订
2022/03/21 日漫
CSS中使用grid布局实现一套模板多种布局
2022/07/15 HTML / CSS