tags:

views:

433

answers:

4

I want to redirect the user to a certain page if he/she has javascript disabled. I tried this code:

<noscript><?php url::redirect('controller/method'); ?></noscript>
// url::redirect is much like the location header

to no avail...

How do I do this?

+4  A: 

There is no way to do a redirect based on if javascript is disabled. Why not do the opposite - redirect if javascript is enabled?

<script>
   window.location = "...";
</script>
Daniel A. White
I want to prevent a page from being viewed without javascript...any ideas on how to do that?
Ygam
You could load the page content with AJAX. Question: why would you want to do that?
Daniel A. White
because the page is javascript-dependent, kind of like adding a google map to the database
Ygam
This logic is nice and straightforward... Show the user the "dumb" page and if they meet all the pre-reqs, give 'em the good stuff. +1
jheddings
+2  A: 

Since the headers have already been sent, you'll need to use standard HTML markup:

<noscript>
  <meta http-equiv="refresh" content="0;url=noscript.html">
</noscript>

Trying this on both Firefox and IE seems to work well... With JavaScript enabled, the <meta> tag is ignored. When it is disabled, the browser redirects to noscript.html.

jheddings
This is not a catch all solution.
Daniel A. White
Could you explain more?
jheddings
although it works, validators hates that. noscript cannot appear in the head element, meta cannot appear in the body element. meta cannot appear in the noscript element. XHTML 1.0 and HTML 4.0.
thephpdeveloper
just another brute hack.
thephpdeveloper
While I always appreciate the purist solution, I find that there are many "hacks" required to get web pages to render on modern browsers. Not an excuse, I'm just sayin' ...
jheddings
Browsers can choose not to redirect.
Daniel A. White
A: 

You can't use meta refresh tag then remove it using Javascript because the browser is set to redirect upon reading the meta refresh tag and it's too late for JS to manipulate it.

The only way is to either do what Daniel suggested, or to show up a link when there's no Javascript:

<noscript>
<a href="">Click here to continue</a>
</noscript>

Or you can try to fail gracefully: http://stackoverflow.com/questions/1577757/do-you-plan-for-javascript-being-off/1577784#1577784

thephpdeveloper
You don't need to use JS to manipulate the `<meta>` tag... It is ignored if contained in the `<noscript>` block.
jheddings
A: 

Add this as the first element in your body, style it to suit, and perhaps offer a link inside of it to a noscript page:

<div onload="return false;">
    <!-- PAGE CONTENTS -->
</div><noscript>JAVASCRIPT IS REQUIRED TO VIEW THIS PAGE</noscript>

I use a more verbose method of this (that basically makes it appear as a modal dialog and all pretty) on pages where a client does not allow me to use more compatable means. I've tested in IE6+ and all other major browsers with great success.

Kevin Peno