javascript set cookie with expire time

后端 未结 8 741
日久生厌
日久生厌 2020-11-27 14:03

I am setting a cookie by Javascript and it is working fine but it is not taking the expire time I am giving. It keeps on taking session value regardless of what I give, belo

8条回答
  •  借酒劲吻你
    2020-11-27 14:30

    I use a function to store cookies with a custom expire time in days:

    // use it like: writeCookie("mycookie", "1", 30)
    // this will set a cookie for 30 days since now
    function writeCookie(name,value,days) {
      if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
      }
      else var expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
    }
    

提交回复
热议问题