How to detect that JavaScript or Cookies are disabled in the user\'s browser and notify him any help ?
As the cookie detection didn't work in IE 11, I suggest the Modernizr approach:
function areCookiesEnabled() {
try {
document.cookie = 'cookietest=1';
var cookiesEnabled = document.cookie.indexOf('cookietest=') !== -1;
document.cookie = 'cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
return cookiesEnabled;
} catch (e) {
return false;
}
}
https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cookies.js
This is the easiest way
if (navigator.cookieEnabled) {
document.write("Cookies Enabled");
} else
{
document.write("Oops Cookies Not Enabled");
}
Check if Cookies Enabled in Your Browser:
<br />
This JavaScript function has always worked for me:
if (typeof areCookiesAllowed !== 'function')
{
function areCookiesAllowed()
{
var cookies_allowed = navigator.cookieEnabled;
if (!cookies_allowed)
{
try
{
var cur_dt = new Date();
var cur_tm = cur_dt.getTime();
document.cookie = 'cookie_test_' + cur_tm + '=1';
cookies_allowed = document.cookie.indexOf('cookie_test_' + cur_tm + '=') !== -1;
document.cookie = 'cookie_test_' + cur_tm + '=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
catch(err)
{
return false;
};
};
return cookies_allowed;
};
};
if (typeof areCookiesAllowed !== 'function')
{
function areCookiesAllowed()
{
var cookies_allowed = navigator.cookieEnabled;
if (!cookies_allowed)
{
try
{
var cur_dt = new Date();
var cur_tm = cur_dt.getTime();
document.cookie = 'cookie_test_' + cur_tm + '=1';
cookies_allowed = document.cookie.indexOf('cookie_test_' + cur_tm + '=') !== -1;
document.cookie = 'cookie_test_' + cur_tm + '=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
catch(err)
{
return false;
};
};
return cookies_allowed;
};
};
var result_elmt = document.getElementById("result");
var result;
if (areCookiesAllowed() === true)
{
result = 'Congratulations! Cookies are enabled in this Web Browser and on this website!';
}
else
{
result = 'Aww Snap! Unfortunatly cookies are NOT enabled in this Web Browser or are disabled on this website.';
};
result_elmt.innerHTML = result;
alert(result);
<span id="result"></span>