//保存cookie
function setCookie(cname, cvalue){
console.log("cname="+cname+"$$$"+"cvalue="+cvalue)
if (cvalue != null && cvalue != "") {
var exdays = 10; //10天过期
var exdate = new Date();
exdate.setTime(exdate.getTime()+(exdays*24*60*60*1000));
document.cookie = cname+"="+escape(cvalue)+"; expires="+exdate.toGMTString();
}
}
//获取cookie
function getCookie(cname){
var name = cname+"=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name) != -1) {
return unescape(c.substring(name.length, c.length));
}
}
return "";
}
//移除cookie
function delCookie(cname){
var cvalue = getCookie(cname);
if (cvalue != null && cvalue != "") {
var exdate = new Date();
exdate.setTime(exdate.getTime() - 10000); //设置为一个过去的时间就自动过期
document.cookie = cname+"="+escape(cvalue)+"; expires="+exdate.toGMTString();
}
}
js操作cookie
于 2019-11-04 19:54:44 首次发布