Help Save Code2care! 😢

I've lost 99% of my revenue to AdBlockers & AI. Your support could be the lifeline that keeps this passion project alive!

Buy Code2care a Coffee QR Code

Scan to Buy Me A Coffee and help me continue coding for you!

Detect if Cookies are enabled using JavaScript


If you are implementing cookies for your web project you need to know first that are cookies enabled or disabled by the user web browser. This can be done with the help of cookieEnabled available in window.navigator object.

On the load of the webpage we can call a javascript function that checks if cookies are enabled or not.

<!DOCTYPE html>
<html>
<head>
<title>Check if cookies are enabled or disabled</title>

<script type="text/javascript">

function checkCookiesStats() {

 if(navigator.cookieEnabled) {

   document.getElementById("check").innerHTML =
    "Cookies are enabled!!";
  }

  else {
   document.getElementById("check").innerHTML =
    "Cookies are disabled!!";
  }
}

</script>

</head>

<body onload="checkCookiesStats()">

<h2>Check Cookies stats</h2>

<p id="check"></p>

</body>
</html>
Author Info:

Rakesh (He/Him) has a Masters Degree in Computer Science with over 15+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright Code2care © 2024 | Privacy Policy | About Us | Contact Us | Search