//创建cookie 键 名 到期时间
function setCookie(key, val, time) {
var date = new Date();
date.setDate(date.getDate() + time);
document.cookie = `${key}=${val};expires=${date}`;
}
//找到cookie
function getCookie(key) {
var arry = document.cookie.split('; ');
for (var item of arry) {
var newArry = item.split('=');
if (newArry[0] == key) {
return newArry[1];
}
}
}
//删除cookie
function removeCookie(key) {
//此时this指向widow
this.setCookie(key, '', -1);
}
//清空cookie
function clearCookie() {
var arry = document.cookie.split('; ');
for (var item of arry) {
var newArry = item.split('=');
//此时this指向widow
this.setCookie(newArry[0], '', -1);
}
}
cookie的封装
最新推荐文章于 2024-07-19 17:37:28 发布