Check cookies are enabled or disabled on the browser :
In web applications sometimes we need to save some information on client side, to save data on client side the most popular option is cookies. But the user can enable or disable cookies on the browser. So in case if user has disabled cookies you can lost the information. So in case when you want to save data in cookies you can first check the browser that cookies are enabled or not.
So to check the browser has cookies enabled or not we can use the property 'cookieEnabled' of 'navigator' object. The 'cookieEnabled' property returns the true and false. If cookies are enabled then it returns 'true' otherwise 'false'.
Syntax of using cookieEnabled property :
navigator.cookieEnabled
Example of using cookieEnabled property : Now in this example I will show how to use the cookieEnabled property to know the browser has enabled or disabled the cookies.
Sample.html
<!DOCTYPE html>
<html>
<body>
<p>Is your browser has enabled cookies? To check press the Check button</p>
<button onclick="isBrowserEnableCookie()">Check</button>s
<p id="container"></p>
<script>
function isBrowserEnableCookie() {
container = document.getElementById("container");
container.innerHTML = navigator.cookieEnabled;
}
</script>
</body>
</html>
The above HTML will print the True or False. If the cookies are enabled then it will print TRUE and if not then FALSE.
0 Comment(s)