Detect if JavaScript is enabled, jump if no JS + W3C validator
A lot of people – fear of viruses, browser bugs, adverts … – simply turn off the JavaScript function of web browsers. This is a very great problem for today’s dynamic web pages or web applications, because in this case they could become completely ineffective. The user doesn’t think of he/she turned off JavaScript, but rather blame the producer of the page. This method gives a solution for this. Redirect the user to a static page where we can explain why it is wrong to turn off the JavaScript ![]()
What we want to achieve
- We want to ensure, when JavaScript is turned off, redirect the browser to a static page or to an error page.
- We would like to keep the XHTML compatibility
- Work in all popular browser (Mozilla Firefox, Internet Explorer, 6-7-8, Opera, Safari, Google Chrome)
I’ve never seen a solution posted anywhere like this, it’s my own idea. It’s a dirty hack, but it works ![]()
The way to the solition
In order to be able to jump without JS, the following meta tag can be used:
<meta http-equiv="refresh" content="0; url=nojs.html" />
If we place the above line to the header of the page, the browser immediately jump to the nojs.html.
It would be an obvious solution if you have JavaScript, you can erase the above line from the DOM and you are done. But unfortunately, it won’t work, because the browser loads and compiles meta tags before the JavaScript can remove it…
Another obvious solution is to enclose the above line with <noscript> </ noscript> tags. The problem with this solution is not standard compliant, maybe even not works in all browser. But it was in our terms that we must care of the XHTML standard.
The solution
First, here is the code and then explain in detail:
<!-- *** Javascript Detect Hack *** -->
<script type="text/javascript"><!--
document.write("<\/script><script>/*");
//--></script>
<meta http-equiv="refresh" content="0; url=nojs.html" />
<script type="text/javascript">/* */</script>
<!-- ********* End of hack ******** -->
How does it work? If there is JavaScript, the script block before <meta> tag place the meta tag to a comment block, which won’t run this way.
Line by line (without the first and the last line of comment):
- The start of the script block. If there is JavaScript, it will run. The
“<!-” HTML comment at the beginning has to be there, because the script
contains “<”, “>” signs, which can’t be in valid XHTML, and when the browser doesn’t know about <script>(or it is turned off) then it will miss this section. - We close the <script> block then open a new one with a start of a Javascript block comment. This will hide the meta tag if the JavaScript is enabled.
- This closes the first JavaScript block. When there is JavaScript this will be in comment, so won’t close anything. This must be in here because of the validator.
- This will redirect the browser if there is no JS.
- This line starts a new JS block. This is also in the JS comment, when JS is enabled until the “*/” characters. The “/*” is for the validator. And the last is the </script>, which close the dynamically opened script tag or if the JavaScript is disabled it closes the <sctipt> started the start of the line.
The point is that there are two levels and both level is correct without the other. The first level is when JavaScript is enabled, and the code is dynamically changed, the
other level is when there is no JS and the code is XHTML compliant.
A further problem
The user can turn off the meta refresh, although it is very uncommon.
Example
This is a working example: http://web2.bitbaro.hu/jshack/
Lia
kedd augusztus 4th, 2009 03:18This is not *live* yet (although you can get to it, no testing server.. sigh). Anyway, thank you. I was banging my head trying to figure out how to get this to validate and get my no-javascript style in that I needed.
Eventually this will be http://www.creighton.edu.
Thanks!
Lia
Wedge
kedd augusztus 4th, 2009 04:07Hi Lia,
I updated the article with a working example. You can see that is a valid XHTML 1.0 Strict, like your site.
lacika
csütörtök augusztus 27th, 2009 07:50negyedik pr
man and van
péntek október 16th, 2009 11:58great i dont understand it but it seem to be from web pages optimization
Paul Morris
szombat október 17th, 2009 07:49Thanks for this. Exactly what I was looking for on my drupal site.
man and van
hétfő február 22nd, 2010 09:23man and van great stuff this really helped me
cheap ugg
hétfő március 1st, 2010 08:02Your article is really exciting, I like it, I hope you can be properly update your zoom.
Laurie
szombat október 30th, 2010 09:05Great Stuff!
On one of my Ajax sites, I use meta refresh to update data for non-js browsers. For js-enabled browsers I needed a way to turn off the meta refresh as data is updated the Ajax way.
Finally after hours of googling (and a couple of beers), I found your solution.
I changed line 5 to make my validator happy.
document.write(‘/*’);
Thanks again.
Laurie
szombat október 30th, 2010 09:09Looks like my code change got clobbered.
I had to add type equals text slash javascript to the second opening script tag.
Cheers
seobaglyak
szombat április 9th, 2011 03:24Köszi! Ez igy valóban működik…
Landry Adkins
csütörtök június 9th, 2011 03:01I’m extremely inspired with your writing skills and even with the layout on your own weblog. Is this a paid subject matter or did anyone customize it on your own? Either way keep up to date the nice top quality writing, it’s rare to peer a nice weblog like that one nowadays..
Ken Irwin
péntek június 17th, 2011 08:58This is brilliant! I would like to mention this code in an article I’m writing and I would like to be able to give you credit appropriately. I don’t see you name on the website anywhere?
Would you please reply (either by email or here in the comments) to let me know how I can give you credit?
Many thanks for this wonderful solution. I’m afraid I’ve been using illegal noscript tags to achieve this effect before!
Petike
csütörtök június 30th, 2011 01:32Some validators and robots detect with this script the webpage is redirected…
(
This cause SEO problems…