javascript tutorial - [Solved-5 Solutions] How to detect javascript is disable - javascript - java script - javascript array
Problem:
There was a post this morning asking about how many people disable javascript. Then we began to wonder what techniques might be used to determine if the user has it disabled. Anyone know of some short/simple ways to detect if Javascript is disabled? my intention is to give warning that the site is not able to function properly without the browser having JS enabled, eventually we would want to redirect them to content that is able to work in the abscence of JS, but WE need this detection as a place holder to start.
Solution 1:
- We assume that you're trying to decide whether or not to deliver JavaScript-enhanced content. The best implementations degrade cleanly, so that the site still operates without JavaScript. we also assume that we mean server-side detection, rather than using the
<noscript>
element for an unexplained reason. - There is not a good way to perform server-side JavaScript detection. As an alternative it is possible to set a cookie using JavaScript, and then test for that cookie using server-side scripting upon subsequent page views. However this would not be suitable for deciding what content to deliver as it would not be able to distinguish visitors without the cookie from new visitors or visitors who are blocking cookies.
Solution 2:
- I'd like to add my .02 here. It's not 100% bulletproof, but we think it's good enough.
- The problem, for me, with the preferred example of putting up some sort of "this site doesn't work so well without Javascript" message is that we then need to make sure that our site works okay without Javascript. And once you've started down that road, then we start realizing that the site should be bulletproof with JS turned off, and that's a whole big chunk of additional work.
- So, what we really want is a "redirection" to a page that says "turn on JS, silly". But, of course, we can't reliably do meta redirections. So, here's the suggestion:
...where all of the content in our site is wrapped with a div of class "pagecontainer". The CSS inside the noscript tag will then hide all of our page content, and instead display whatever "no JS" message we want to show. This is actually what Gmail appears to do...and if it's good enough for Google, it's good enough for my little site.
Solution 3:
This is what worked for me: it redirects a visitor if javascript is disabled
Solution 4:
If our use case is that we have a form (e.g., a login form) and our server-side script needs to know if the user has JavaScript enabled, we can do something like this:
This will change the value of js_enabled to 1 before submitting the form. If our server-side script gets a 0, no JS. If it gets a 1, JS!
Solution 5:
- We can use a simple JS snippet to set the value of a hidden field. When posted back we know if JS was enabled or not.
- Or we can try to open a popup window that we close rapidly (but that might be visible).
- Also we have the NOSCRIPT tag that we can use to show text for browsers with JS disabled.