cookie

作者: 給我小鱼干 | 来源:发表于2018-11-08 15:58 被阅读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
    }

相关文章

网友评论

      本文标题:cookie

      本文链接:https://www.haomeiwen.com/subject/bwpwxqtx.html