Is there any way to check if the client accepts cookies only with javascript code?
+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
2009-04-08 21:05:48
Why `document.cookie.length > 1` and not `document.cookie.length >= 1`?
deamon
2010-07-22 15:29:49