Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid way to add noscript in head for wrapping redirect

Tags:

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 the head 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?

like image 532
Anthony Avatar asked Feb 16 '10 03:02

Anthony


2 Answers

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.

like image 128
Doug Neiner Avatar answered Sep 27 '22 21:09

Doug Neiner


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.

like image 20
Spycho Avatar answered Sep 27 '22 19:09

Spycho