javascript set cookie with expire time

后端 未结 8 608
日久生厌
日久生厌 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:13

    Below are code snippets to create and delete a cookie. The cookie is set for 1 day.

    // 1 Day = 24 Hrs = 24*60*60 = 86400.
    
    1. By using max-age:

      • Creating the cookie:

      document.cookie = "cookieName=cookieValue; max-age=86400; path=/;";
      
      • Deleting the cookie:

      document.cookie = "cookieName=; max-age=- (any digit); path=/;";
      
    2. By using expires:

      • Syntax for creating the cookie for one day:

      var expires = (new Date(Date.now()+ 86400*1000)).toUTCString();
      document.cookie = "cookieName=cookieValue; expires=" + expires + 86400) + ";path=/;"
      
    0 讨论(0)
  • 2020-11-27 14:16
    document.cookie = "cookie_name=cookie_value; max-age=31536000; path=/";
    

    Will set the value for a year.

    0 讨论(0)
  • 2020-11-27 14:17

    I'd like to second Polin's answer and just add one thing in case you are still stuck. This code certainly does work to set a specific cookie expiration time. One issue you may be having is that if you are using Chrome and accessing your page via "http://localhost..." or "file://", Chrome will not store cookies. The easy fix for this is to use a simple http server (like node's http-server if you haven't already) and navigate to your page explicitly as "http://127.0.0.1" in which case Chrome WILL store cookies for local development. This had me hung up for a bit as, if you don't do this, your expires key will simply have the value of "session" when you investigate it in the console or in Dev Tools.

    0 讨论(0)
  • 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=/";
    }
    
    0 讨论(0)
  • 2020-11-27 14:31

    I think its ok. I've set time to 1000*36000. toGMTString is deprecated please use toUTCString instead.

    function display() { 
      var now = new Date();
      var time = now.getTime();
      var expireTime = time + 1000*36000;
      now.setTime(expireTime);
      var tempExp = 'Wed, 31 Oct 2012 08:50:17 GMT';
      // document.cookie = 'cookie=ok;expires='+now.toUTCString()+';path=/';
      document.cookie = 'cookie=ok;expires='+now.toGMTString()+';path=/';
      //console.log(document.cookie);
    }
    

    expiration

    0 讨论(0)
  • 2020-11-27 14:31

    Here's a function I wrote another application. Feel free to reuse:

    function writeCookie (key, value, days) {
        var date = new Date();
    
        // Default at 365 days.
        days = days || 365;
    
        // Get unix milliseconds at current time plus number of days
        date.setTime(+ date + (days * 86400000)); //24 * 60 * 60 * 1000
    
        window.document.cookie = key + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
    
        return value;
    };
    
    0 讨论(0)
提交回复
热议问题