CancelImage Upload

How to find out if a browser has cookie enabled with JavaScript

If your website makes use of cookies, you will no doubt need to find out if your visitor has actually still got them enabled on their browser. If they have you can carry on with business as usual. If not however you may want to go down a different path in order to prevent script errors.

Here then is how you can find out whether or not a browser's cookie property is enabled or disabled. This status can be detected using the following code:

if(navigator.cookieEnabled)
{

           //Your code for when cookies are enabled.

}
else
{

           //Your code for when cookies have been disabled.

}

Based on the code above you can see that the cookiesEnabled path will either return true or false, and based on this you can guide in your relevant actions. Here is another example:

<html>
<head>
<title>How to find out if a browser has cookie enabled with JavaScript</title>
<script language="JavaScript">

function getCookieStatus()
{

           var cookieStatus = document.getElementById("cookieStatus");

           if(navigator.cookieEnabled)
           {

                      cookieStatus.innerHTML = "Cookies are enabled.";

           }
           else
           {

                      cookieStatus.innerHTML = "Cookies have been disabled.";

           }


}

</script>

</head>
<body onload="javascript:getCookieStatus();">

<span id="cookieStatus"></span>

</body>
</html>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com