// 设置cookie
function setCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ";expires = " + exdate.toGMTString() + ";path = /;";
}
// 获取cookie
function getCookie(name) {
var arr;
var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : '';
}
// 删除cookie
function delCookie(name,domain,value = '') {
var exdate = new Date();
exdate.setDate(exdate.getDate() - 1);
if (name != null && domain) {
document.cookie = name + "=" + escape(value) + ";expires = " + exdate.toGMTString() + ";path = /;domain=" + domain;
} else {
document.cookie = name + "=" + escape(value) + ";expires = " + exdate.toGMTString() + ";path = /;";
}
}
这里注意工作中遇到的一个小问题domain,在我清楚cookie的时候不关怎么清除都清不掉,这个就和domain有关系了,在我登录的时候后台给浏览器写入了一个cookie domain为“.zgbxjj.com”,而我的当前域名为“test.zgbxjj.com”,在我清除cookie的时候,默认清除当前域名下的cookie(当前域名为 test.zgbxjj.com)所以一直清除不掉,这里我们修改domain为“.zgbxjj.com”就可以清楚掉了。
// 设置cookie
function setCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ";expires = " + exdate.toGMTString() + ";path = /;";
}
// 获取cookie
function getCookie(name) {
var arr;
var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : '';
}
// 删除cookie
function delCookie(name,domain,value = '') {
var exdate = new Date();
exdate.setDate(exdate.getDate() - 1);
if (name != null && domain) {
document.cookie = name + "=" + escape(value) + ";expires = " + exdate.toGMTString() + ";path = /;domain=" + domain;
} else {
document.cookie = name + "=" + escape(value) + ";expires = " + exdate.toGMTString() + ";path = /;";
}
}