Valid way to add noscript in head for wrapping redirect
Solution 1
I am not sure why you need to redirect to another page instead of just showing a message. I use JS and a little CSS to handle these situations for me. Something like this:
<head>
....
<script type="text/javascript"> document.documentElement.className += " js"</script>
<link rel="stylesheet" type='text/css' href="css/layout.css" media="all" />
</head>
<body>
<div id="noscript">Please enable JavaScript, then refresh this page. JavaScript is required on this site</div>
<div id="wrapper">
...
</div>
</body>
Then in layout.css
:
#wrapper { display: none } /* Hide if JS disabled */
.js #wrapper { display: block } /* Show if JS enabled */
.js #noscript { display: none } /* Hide if JS enabled */
By doing it this way, the class is applied to the html
element before the page is rendered so you won't get a flicker as the non-JS content is swapped out for the JS content.
Solution 2
Doug's solution is pretty good, but it has a few drawbacks:
- It is not valid to have a class attribute on the html element. Instead, use the body.
- It requires that you know what display type to set the element to (i.e. "
.js #wrapper { display: block }
").
A simpler, more valid and flexible solution using the same approach could be:
<html>
<head>
<!-- put this in a separate stylesheet -->
<style type="text/css">
.jsOff .jsOnly{
display:none;
}
</style>
</head>
<body class="jsOff">
<script type="text/javascript">
document.body.className = document.body.className.replace('jsOff ','');
</script>
<noscript><p>Please enable JavaScript and then refresh the page.</p></noscript>
<p class="jsOnly">I am only shown if JS is enabled</p>
</body>
</html>
With this, it's valid html (no class attribute on the html element). It is simpler (less CSS). It's flexible. Just add the "jsOnly" class to any element that you want to only display when JS is enabled.
Solution 3
The <noscript>
tag cannot be in the <head>
, it must be in the <body>
The common practice is to show a message instead of redirecting, as there is no way to redirect only if javascript is disabled.
You could do it the other way around, have the first page be nojs.html, and on that page use javascript to redirect to the main content.
![Ashith](https://i.stack.imgur.com/4Bw9x.jpg?s=256&g=1)
Ashith
I work at a help desk. I have a BA in English and a BS in Radio-TV-Film, both from the University of Texas at Austin. I started dabbling in web design (HTML) in 1998, semantic web design (XHTML, CSS) since 2006, and server-side web design/applications(PHP, MySQL) since 2008. I have managed the LAN and wireless network for my student co-op for a little over a year now, and still find the whole thing nerve-wracking. I have no accredited training in any of the above, but I am very good at asking annoying questions to people who know way more than me. SOreadytohelp
Updated on June 06, 2022Comments
-
Ashith about 2 years
So I was thinking a simple way to deal with javascript being disabled by the browser would be the following:
<head> <title>JavaScript Test</title> <noscript> <meta http-equiv="Refresh" content="1;url=nojs.html" /> </noscript> </head>
And having the
nojs.html
have something like:<p>Return to <a href="jstest.html">test</a> after enabling javascrpt.</p>
At the crash page.
This isn't my preferred method, but it's nice and simple until something more graceful can be worked out for users without javascript.
However, it is not valid to put a
<noscript>
element in thehead
section. The preliminary tests worked anyway, of course, but I'm superstitious when it comes to my code being valid, plus I'd hate for this to actually fail a field test.So is there a valid way to do this? Perhaps wrapping the
noscript
in another element, like an object tag? Or some even simpler way I'm not thinking of? -
mattalxndr almost 14 yearsWhy
<div id="noscript">
and not<noscript>
? You could take that third line out of your CSS. -
Doug Neiner almost 14 years@mattalexx More to keep it consistent. Also, very few elements are valid to use inside
<noscript>
tags, so in this example it would be a fine alternative, but in many cases it would not be. -
τεκ almost 13 yearsThis increases loading time for the normal case, while slightly improving experience for the abnormal case. As such it isn't a good idea.
-
Aidiakapi over 11 yearsCreative solution, but it should be
position: fixed;
instead ofposition: absolute;
. -
mhoff almost 11 yearsIn HTML4.01 this is true, but for HTML5 you can write <noscript> in <head> (using <link>, <style> and <meta> elements only) w3schools.com/tags/tag_noscript.asp
-
codechurn over 9 yearsGreat approach without redirect (and subsequent content flash) and supported in HTML4/5.