可兼容IE的获取及设置cookie的jquery.cookie函数方法 - Web前端
作者:98wpeu发布时间:2026-06-16分类:网页前端技术浏览:61
导读:前言在开发过程中,因为之前有接触过Discuz,就直接拿其common.JS里面的getcookie和setCookie方法来使用,做到后面在使用ie来测试的时候,发现这两个方法子...
前言
在开发过程中,因为之前有接触过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).
* @typeundefined
*
* @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;
}
};
在开发过程中,因为之前有接触过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).
* @typeundefined
*
* @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;
}
};
相关推荐
- jQuery powerFloat万能浮动层下拉层插件使用介绍 - Web前端
- 基于jquery的动态创建表格的插件 - Web前端
- 使用jquery为table动态添加行的实现代码 - Web前端
- ASP.NET jQuery 实例16 通过控件CustomValidator验证RadioButtonList - Web前端
- autoIMG 基于jquery的图片自适应插件代码 - Web前端
- jQuery powerFloat万能浮动层下拉层插件使用介绍 - Web前端
- Jquery下判断Id是否存在的代码 - Web前端
- DIY jquery plugin - tabs标签切换实现代码 - Web前端
- jquery 学习之二 属性 文本与值(text,val) - Web前端
- 基于jquery的simpleValidate简易验证插件 - Web前端
- 网页前端技术排行
-
- 1分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 2【第六章】Foundation之按钮和下拉功能 - Web前端
- 3jQuery编写widget的一些技巧分享 - Web前端
- 4在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 9javascript原生和jquery库实现iframe自适应高度和宽度 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


