可兼容IE的jquery.cookie函数方法
来源:互联网 发布:晨光文具淘宝店 编辑:程序博客网 时间:2024/05/30 04:40
前言
在开发过程中,因为之前有接触过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; }};
- 可兼容IE的jquery.cookie函数方法
- 工作笔记---jQuery在可编辑的div光标处插入内容 兼容IE,火狐,谷歌
- 解决IE兼容的方法
- IE兼容-placeholder的方法
- jquery 返回顶部 IE Quircks 的兼容
- IE兼容Jquery AJAX
- 利用jQuery srollTop()函数实现类似锚点的定位效果-兼容chrome,firefox,IE,safari
- ie可兼容删除节点
- IE兼容setAttribute函数的问题
- 兼容ie的javascript封装EventUtil函数
- 兼容ff和ie的可编辑iframe
- IE FF可兼容的flash播放器
- JS控制的可拖动层代码(兼容IE,FF)
- 兼容IE的javascript跳转(可传递referer)
- 兼容ie的删除js,可获取到referrer连接
- firefox兼容ie的dom方法脚本
- 兼容FF/IE的insertAdjacentHTML方法
- 兼容FireFox/IE的insertAdjacentHTML方法【Ext2】
- 海量数据处理算法—Bit-Map
- Android网络通信库Volley简介
- oracle入门很简单:二、创建oracle数据库
- 复杂大系统需要对服务依赖关系进行管理
- Kendo UI开发教程(16): Kendo MVVM 数据绑定(五) Events
- 可兼容IE的jquery.cookie函数方法
- Unity3D实用工具汇总
- vc6.0工程转成vs2008出现的问题及解决办法(转)
- hdu4245(map)
- 【Cocos2d-X开发笔记】第一期 Cocos2d-X的环境搭建
- android中代码中android:layout_margin同样功能的使用
- ListView的Item中设置按钮
- ogre sample分析(一)
- 函数指针的使用--回调函数