Cookie expiration date

前端 未结 7 1286
梦如初夏
梦如初夏 2020-12-30 01:43

I am not a programmer. I am trying to use a cookie script that remembers the last drop down menu selection.

I found a script that works but it does only a session co

7条回答
  •  梦谈多话
    2020-12-30 02:18

    This is for setting the expiry date in terms of minutes(5 minutes here)
    
    function setCookie(cname, cvalue, exdays) {     
      var d = new Date();
      d.setTime(d.getTime() + exdays * 60 * 1000);
      var expires = "expires="+d.toUTCString();
    
    }
    
    function getCookie(cname) {        
      var name = cname + "=";
      var ca = document.cookie.split(';');
      for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
          c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
          return c.substring(name.length, c.length);
        }
      }
      return "";
    }
    function checkCookie() {
      var user = getCookie("username");
      if (user != "") {
         //your code goes here
      } else {
         //your code goes here
        if (user != "" && user != null) {
          setCookie("username", user, 5);
        }
      }
    }
    

提交回复
热议问题