/ 获取cookie/
function getCookie (name){ //export暴露方法的写法export const getCookie = (name) => {
vararr,reg=newRegExp('(^| )'+name+'=([^;]*)(;|$)')
if(arr=document.cookie.match(reg)) {
returnunescape(arr[2])
}else{
returnnull
}
}
/储存cookie/
function setCookie (cName, value, expiredays) => {
varexdate=newDate()
exdate.setDate(exdate.getDate() +expiredays)
document.cookie=cName+'='+escape(value) +
((expiredays===null) ?'':';expires='+exdate.toGMTString())
}
/删除cookie/
function delCookie(name){
varexp=newDate()
exp.setTime(exp.getTime() -1)
varcval=getCookie(name)
if(cval!=null) {
document.cookie=name+'='+cval+';expires='+exp.toGMTString()
}
}
调用:存储cookie:setCookie('user_name', data.user.user_name, 30) //存储名、存储值、过期时间
获取cookie:getCookie('user_name')
删除cookie:delCookie('user_name')
网友评论