Javascript – Check if the client accepts cookie in javascript


Is there any way to check if the client accepts cookies only with javascript code?

Best Solution

This should do the trick:

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;

This sets a cookie with session-based expiration, checks for it's existence, and then sets it again in the past, removing it.