Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect that JavaScript and/or Cookies are disabled?

Tags:

javascript

How to detect that JavaScript or Cookies are disabled in the user's browser and notify him any help ?

like image 521
Mahmoud Saleh Avatar asked Jan 05 '11 10:01

Mahmoud Saleh


People also ask

How do you check if JavaScript and cookies are enabled?

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.

How do I know if JavaScript is disabled?

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.


2 Answers

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

like image 128
robjmills Avatar answered Oct 27 '22 00:10

robjmills


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; } 
like image 20
Noah Solomon Avatar answered Oct 26 '22 22:10

Noah Solomon