添加获取cookie
2019-04-16 本文已影响0人
給我小鱼干
getCookie(name) {
if (document.cookie.length > 0) {
var cookieStart = document.cookie.indexOf(name + "=")
if (cookieStart !== -1) {
cookieStart = cookieStart + name.length + 1
var cookieEnd = document.cookie.indexOf(";", cookieStart)
if (cookieEnd === -1) cookieEnd = document.cookie.length
//return unescape(document.cookie.substring(cookieStart, cookieEnd))
return decodeURIComponent(document.cookie.substring(cookieStart, cookieEnd))
}
}
return ""
},
addCookie(objName, objValue, objHours) { // 添加cookie
var str = objName + "=" + escape(objValue);
if (objHours > 0) { //为0时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date()
var ms = objHours * 3600 * 1000
date.setTime(date.getTime() + ms)
str += "; expires=" + date.toGMTString()
}
document.cookie = str
},