Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if javascript is disabled?

How to check if the user disabled javascript in his browser? For example: the stackoverflow site shows a red warning div at the top if you disable the javascript in firefox? How to achieve this?

One more thing I noticed: if you disable cookies, stackoverflow doesn't work. It won't let you login when you click the login button. Actually, that's what I assume with my website as well, the user login/session data depends on cookies, UI layout etc.. depends on javascript.

Here I don't want to discuss about the design principle (progressive enhancement etc..), could anyone tell me a good way to check if cookies, javascript are enabled or not? I think it has to be done on server side, right?

Thank you

like image 940
WilliamLou Avatar asked Jan 19 '10 23:01

WilliamLou


People also ask

How do I know if I have disabled JavaScript?

First, click on the "Security" tab. Under the "Web content" section, you'll see a box next to "Enable JavaScript." If the box is unchecked, that means JavaScript has been disabled.

How do I know if an element is disabled or enabled?

Use the disabled property to check if an element is disabled, e.g. if (element. disabled) {} . The disabled property returns true when the element is disabled, otherwise false is returned. Here is the HTML for the examples in this article.


1 Answers

Add a <noscript> tag to the page, and if JavaScript is disabled, the message will show to the user.

You can also detect if JavaScript is enabled based on the success of running JavaScript code

  • add a hidden field and set its default value to false
  • Run some javascript code that sets the value to true
  • When you post back, check the value.

To check for cookies, set a cookie and try to read it. If it succeeds, then cookies are enabled.

like image 130
Gabriel McAdams Avatar answered Oct 06 '22 11:10

Gabriel McAdams