html javascript 设置cookie永不过期

function cookieToJson(str) {
    let cookieArr = str.split(";");
    let obj = {} 
    cookieArr.forEach((i) => {
        let arr = i.split("=");
        obj[arr[0]] =arr[1];
    });
    return obj
  }
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}
var cks = cookieToJson(document.cookie);
for(var p in cks) {
    console.log(p,cks[p])
    writeCookie(p,cks[p])
}

 

posted @ 2021-01-15 16:39  方方和圆圆  阅读(2225)  评论(1编辑  收藏  举报

再过一百年, 我会在哪里?