views:

207

answers:

3

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

+1  A: 

Sure, try setting one and reading it.

Alex JL
+1  A: 

Yep. Here is a link from W3 that shows how: http://www.w3schools.com/JS/js_cookies.asp

+2  A: 

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.

Ben Lowery
Why `document.cookie.length > 1` and not `document.cookie.length >= 1`?
deamon