1.getCookie
function getCookie(key) {
var value = ''
var str = document.cookie.split('; ')
console.log(str);
str.forEach(item =>{
var tmp = item.split('=')
if(tmp[0] === key){
value = tmp[1]
}
})
return value
}
2.setCookie,注意设置cookie和修改cookie一样
function setCookie(key,value,expires) {
var time = new Date()
time.setTime(time.getTime() - 1000 * 60 * 60 * 8 + 1000 * expires)
var str =`${key}=${value};expires=${expires ? time : ''}`
document.cookie = str
}
setCookie('a','1000')
// expires为设置过期时间单位为秒
3.delCookie,删除cookie的原理为设置过期时间为当前时间之前
function delCookie(key) {
setCookie(key,'',-1)
}