How to detect that JavaScript or Cookies are disabled in the user's browser and notify him any help ?
To check whether a setting a cookie is enabled in the browser, you can use the cookieEnabled property in the window. navigator global object in JavaScript. The property will return a Boolean true if cookie enabled and return false if not enabled.
On the web browser menu click on the "Edit" and select "Preferences". In the "Preferences" window select the "Security" tab. In the "Security" tab section "Web content" mark the "Enable JavaScript" checkbox. Click on the "Reload the current page" button of the web browser to refresh the page.
For checking cookies you can use:
function checkCookie(){ var cookieEnabled = navigator.cookieEnabled; if (!cookieEnabled){ document.cookie = "testcookie"; cookieEnabled = document.cookie.indexOf("testcookie")!=-1; } return cookieEnabled || showCookieFail(); } function showCookieFail(){ // do something here } // within a window load,dom ready or something like that place your: checkCookie();
And for checking JavaScript use a <noscript>
tag with some kind of message inside
Update (6/25/18):
A lot of these posts, including mine, are taking snippets from Modernizr. They will all eventually become outdated as the Modernizr code gets updated.
I think the best answer to this question should be to use Modernizr directly.
if (Modernizr.cookies) { // supported } else { // not-supported }
Original Answer (5/11/17):
This is taken straight from Modernizr and works in more browsers than other solutions in this post.
https://github.com/Modernizr/Modernizr/commit/33f00fbbeb12e92bf24711ea386e722cce6f60cc
function checkCookie(){ // Quick test if browser has cookieEnabled host property if (navigator.cookieEnabled) return true; // Create cookie document.cookie = "cookietest=1"; var ret = document.cookie.indexOf("cookietest=") != -1; // Delete cookie document.cookie = "cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT"; return ret; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With