How to check whether JavaScript is enabled or not

My application depends on JavaScript, I want to check whether the browser browser has JavaScript enabled or not and raise a warning message if it is disabled.

+6
javascript
source share
4 answers

In fact, there is a <noscript> tag that you can use to display the contents contained inside when javascript is not available.

Something like:

 <noscript> <div> You must enable javascript to continue. </div> </noscript> 

the div just won't show if they have javascript, and it's pretty easy to determine if javascript is working, regardless of whether you need to do this to check your server to tell it, or use it to perform more advanced functions.

+12
source share

Put the message in the <div> , which is wrapped in the <noscript> . If JavaScript is disabled, the <div> will be displayed as part of the DOM; if the script is included, the div will not be in the DOM.

For example, you can put the following immediately after the opening of the <body> and style it with CSS to have a red background to make it more visible.

 <noscript> <div id="js-warning"> To be able to access all of our features, you need a browser that supports JavaScript, and it needs to be enabled. </div> </noscript> 
+13
source share

β€œRaise a warning message if it is turned off” is a paradox, because if it is turned off, you cannot β€œdo” anything programmatically.

However, you can do it the other way: make this the default message, and JavaScript will close it if it is enabled (for example, setting the visibility of DIVs to hidden),

or you rely on standard browser matching and use the <noscript> tag. Material inside <noscript> is displayed if javascript is not included. BTW, be sure to set the type = "text / javascript" attribute of the script tag.

See also http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.3

+4
source share

The browser may be "JavaScript-capable", but this does not mean that JavaScript has not been disabled by the user or administrator. There is no real way to determine this. Best practices dictate "progressive improvement"; that is, you must first work without JavaScript, and then add JavaScript functions for those (most) that were included.

http://www.alistapart.com/articles/progressiveenhancementwithjavascript/

http://www.webcredible.co.uk/user-friendly-resources/dom-scripting/progressive-enhancement.shtml

Avoid hacking solutions and remember that there are accessibility issues for people with screen readers. <NoScript> content is only displayed if JavaScript is disabled. Most screen reader users have JavaScript enabled, so they will see your inaccessible script, not the <noscript> content.

+2
source share

All Articles