If you enjoyed this site, please consider donating $3. Any amount is appreciated. Thanks!

JavaScript Tutorial: Checking Cookies

How to check if the browser has cookies turned off?

Check the property “navigator.cookieEnabled”, which directly tells you whether the browser has cookies enabled.

If your browser has cookie enabled, you'll see “Yay, cookies!” below, else you'll see “O, no cookies!”.

Here's the code:

if (navigator.cookieEnabled) {
 document.write('<p><span style="color:blue">Yay, cookies!<\/span><\/p>');
} else {
 document.write('<p><span style="color:blue">O, no cookies!<\/span><\/p>');
}

Setting and Getting Cookie

You can always test whether the browser can accept a cookie by setting a cookie, and see if it is taken. You set a cookie by “document.cookie”.

https://developer.mozilla.org/en/DOM/document.cookie

2005-12
© 2005 by Xah Lee.