首页 > 其他分享 >CookieUtil

CookieUtil

时间:2023-01-29 22:23:08浏览次数:34  
标签:domain name expires cookieText path CookieUtil secure

const CookieUtil = {
    get(name){
        let cookieName = `${encodeURIComponent(name)}=`
        let cookieStart = document.cookie.indexOf(cookieName)
        let cookieValue = ''
        if(cookieStart > -1){
            let cookieEnd = document.cookie.indexOf(';', cookieStart)
            if(cookieEnd === -1){
                cookieEnd = document.cookie.length
            }
            cookieValue = `${decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd))}`
        }
        return cookieValue
    },
    set(name, value, expires, path, domain, secure){
        let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
        if(expires instanceof Date){
            cookieText += `;expires=${expires.toGMTString()}`
        } else {
            cookieText += `;expires=${new Date(expires).toGMTString()}`

        }
        if(path){
            cookieText += `;path=${path}`
        }
        if(domain){
            cookieText += `;domain=${domain}`
        }
        if(secure){
            cookieText += `;${secure}`
        }
        document.cookie += cookieText
    },
    unset(name, path, domain, secure){
        this.set(name, '', new Date('1970/01/01'), path, domain, secure)
    }
}

  

标签:domain,name,expires,cookieText,path,CookieUtil,secure
From: https://www.cnblogs.com/zhenjianyu/p/17073966.html

相关文章