解决IE7 & IE8 存储cookie问题
最近在做一个项目,需要兼容比较低版本的浏览器,现在竟然还有人用xp系统的IE7 !!!!听到这个消息的时候我整个人都不好了。
其中某个功能需要用cookie存储和清除,发现在ie8及以一下没有效果 。解决方法:
/**
* 设置cookie
* @param {String} key
* @param {String} value
* @param {Number} expires 单位为秒
* @param {String} path 路径
* @param {String} domain 主机
* @param {Boolean} secure 安全级别
*/
function setCookie(key, value, expires, path, domain, secure) {
// key无效则不做任何操作
if (!key) {
return;
}
// 所有cookie的key都小写化处理
key = key.toLowerCase();
key = key + '=' + escape(value) + ";";
expires = expires ? ("expires=" + new Date(new Date().getTime() + expires * 1000).toGMTString()) + ";" : "";
path = !!path ? "path=" + path + ";" : "path=/;";
domain = domain ? ("domain=" + domain) + ";" : "";
secure = secure ? "secure=true;" : "";
document.cookie = [key, expires, path, domain, secure].join("");
}
//清除cookie
function clearCookie(name) {
var e = new Date();
e.setTime(e.getTime() - 1);
var b = getCookie(name);
document.cookie = name + "=" + name + ";path=/; domain=" + window.location.host + "; expires=" + e.toGMTString()
}
真实有效哦!!!!!
版权声明:本文为weixin_39330484原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。