How To Test Cookie Support using JavaScript

A cookie is a small object allows servers to store user’s information on user machine. Cookies are stored in the partition assigned to the web server’s domain. All cookies for that domain are sent in every request to that web server. When a user visits a page, his name can be stored in a cookie. Next time when user visits, the cookie recognizes user on the server. Cookies help in to identify user uniquely for session management.

Using Javascript we can detect if the cookie support is enabled or not. The following example demonstrates this:

Test Cookie Support using JavaScript Example

<!DOCTYPE html>
<head>
<TITLE>Example to detect Cookie Support</TITLE>
</head>
<body>
<script type="text/javascript">
var cookieEnabled=(navigator.cookieEnabled)? true : false
document.writeln("<b>cookieEnabled status </b>--"+cookieEnabled);

//if (cookieEnabled) //if cookies are enabled on client's browser
//do whatever

</script>

</body>
</html>

Cookie Support using JavaScript Demo

  • Save the file as cookiesupportexample.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser. Note that the browser must support HTML specification.

When the execution process is completed successfully we will get the following output, displaying if the cookie is enabled or not:

How To Test Cookie Support using JavaScript

Comments

comments

Speak Your Mind

*