views:

642

answers:

7

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?

A: 

Can you set a header to Redirect?

In PHP: header('Location: http://www.example.com/index.php');

Note that you must set headers before any output.

waiwai933
I think the OP's whole concept was to detect JS. A PHP redirect would not accomplish that.
Doug Neiner
I could, but I wouldn't want to unless js was disabled, which I couldn't tell reliably server-side. (I could try it with ajax but js would be disabled, so no luck their either).
Anthony
A: 

If you truly want a valid way to do it, make your main page the nojs.htm page and use JS to hide all content before it's shown to the user and immediately redirect to the real main page using javascript.

Sam
+7  A: 

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.

Doug Neiner
Why `<div id="noscript">` and not `<noscript>`? You could take that third line out of your CSS.
mattalexx
@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.
Doug Neiner
@Doug, +1 Thanks for the response.
mattalexx
@mattalexx Sure, no problem! It was a great question.
Doug Neiner
+2  A: 

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.

Peter Di Cecco
A: 

I would go with the solution Doug Neiner has provided. You most definitely don't want to do a redirect using the noscript because you will find that lots of search engines will index the "No Javascript Enabled" page as the home page. I did that and learnt the hard way :(

Alex
A: 

I like Doug's solution. However, if you need to redirect, I would remember that while there is a spec and a standard, the world of web browsers is a dirty, imperfect world. Whether or not something is allowed by the spec is not as important as whether or not it works in the set of browsers you care about.

Just look at the source code of any major site... Most of them won't validate I'd bet :)

levik
A: 

Can you give the meta tag an id property, then remove the tag via JS?

Tracker1