Js读写Cookie

// 读取cookie
function getCookie(key) {
   if (document.cookie.length > 0) {
      var start = document.cookie.indexOf(key + "=")
      if (start !== -1) {
         start = start + key.length + 1
         var end = document.cookie.indexOf(";", start)
         if (end === -1)
            end = document.cookie.length
         return unescape(document.cookie.substring(start, end))
      }
   }
}

// 设置cookie
function setCookie(key, value, second) {
   var cookie = key + "=" + escape(value) + ";";
   if (second !== undefined && second !== '') {
      cookie += "Max-Age=" + second + ";";
   }
   document.cookie = cookie
}

如果文章对你有帮助, 你可以: 请我喝牛奶

一条评论

Leave a Comment.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据