Check if httponly cookie exists in Javascript

前端 未结 4 1790

As the question says can you find out if a cookie exists within Javascript if it is a HttpOnly? I don\'t need to access the information inside of it, just know it has one.

4条回答
  •  半阙折子戏
    2020-12-16 15:11

    You can indirectly check to see if it exists by trying to set it to a value with javascript if it can't be set, then the HTTP Only Cookie must be there (or the user is blocking cookies).

    function doesHttpOnlyCookieExist(cookiename) {
      var d = new Date();
      d.setTime(d.getTime() + (1000));
      var expires = "expires=" + d.toUTCString();
    
      document.cookie = cookiename + "=new_value;path=/;" + expires;
      if (document.cookie.indexOf(cookiename + '=') == -1) {
        return true;
      } else {
        return false;
      }
    }
    

提交回复
热议问题