How to Check if Cookies are Enabled or Disabled

Use-Cases of this tutorial

  • Know how to check whether cookies are currently enabled or disabled using Javascript.
  • Know about navigator.cookieEnabled property.

Without cookies being enabled in the browser, some web applications may not work as expected. To prevent such cases, navigator.cookieEnabled property can be used to do a prior check whether cookies are enabled or not.

Cookies can be used for tracking purposes, so some users may prefer to disable them in their browser. However if any web application is dependent on cookies, then it won't perform as expected.

So before trying to read or write cookies with Javascript, it is better to check whether cookies are enabled or not. If cookies are disabled, then the user can be informed to enable cookies in order to proceed further, or the application may follow a different logic — like not using any cookies at all.

Instead of automatically assuming that cookies must be enabled by default, and then resulting in a crash later on, it is better to do a prior check.

The navigator.cookieEnabled property can be used to check whether cookies are enabled or disabled. This returns true if cookies are enabled, or false if cookies are disabled.

if(navigator.cookieEnabled) {
	// cookies are enabled
	// read, write and delete cookies
}
else {
	// cookies are disabled, show an error message to the user, or follow other alternative
}

Live Demo

Are cookies enabled ?
Loading Comments