How to check if the browser supports JavaScript cookies?

I want to check with JavaScript whether the browser supports cookies or not. The following code works with Internet Explorer 8 and Firefox 3.6, but not with Google Chrome 5.

function areCookiesEnabled() {
  document.cookie = "__verify=1";
  var supportsCookies = document.cookie.length > 1 &&
                        document.cookie.indexOf("__verify=1") > -1;
  var thePast = new Date(1976, 8, 16);
  document.cookie = "__verify=1;expires=" + thePast.toUTCString();
  return supportsCookies;
}

if(!areCookiesEnabled()) 
    document.write("<p>Activate cookies!</p>");
else
    document.write("<p>cookies ok</p>");

Chrome displays the message "Activate cookies!" regardless of my cookie settings. But if I disable cookies, Chrome tells me that the cookie cannot be set.

Any idea how to check the availability of a cookie with JavaScript in Chrome?

+3
source share
2 answers

, cookie, . .

+2

, , , :

function areCookiesEnabled() {
  document.cookie = "__verify=1;expires=" + new Date(1976, 8, 16).toUTCString();
  return  (document.cookie.length > 1);
}

if(!areCookiesEnabled()) 
    document.write("<p>Activate cookies!</p>");
else
    document.write("<p>cookies ok</p>");

, cookie javascript, .

+2

Source: https://habr.com/ru/post/1756008/


All Articles