// 保存cookie(添加和修改)
// 参数:
// 键
// 值
// 保存时长:单位是天
// 可以访问的路径
// 可以访问域名
// 返回值:无
function saveCookie(key,value,dayCount,path,domain){
path = path || "";
domain = domain || "";
let d = new Date();
d.setDate(d.getDate() + dayCount);
let str = `${key}=${encodeURIComponent(value)};expires=${d.toGMTString()}`;
if(path!=""){
str += `path=${path}`;
}
if(domain!=""){
str += `domain=${domain}`;
}
document.cookie = str;
}
// 获取cookie
// 参数:
// 键
// 返回值:
// 键对应的值;
function getCookie(key){
let str = decodeURIComponent(document.cookie); //username1=小白; userpass=666; username=杨雯静
let arr = str.split("; "); //["username=小白","userpass=666"]
let result = "";
for(let i=0;i<arr.length;i++){
if(arr[i].startsWith(key+"=")){
// result = arr[i].split("=")[1];
[,result] = arr[i].split("="); //["username","小白"]
break;
}
}
return result;
}
// 删除cookie
// 参数:
// 键
// 返回值:无
function removeCookie(key){
saveCookie(key,"byebye",-1);
}