views:

34

answers:

3

I have a large, dynamically generated, single page website that heavily relies on javascript. I want to have a fallback for people without javascript.

Each version must have different php code, so basically I need an entirely different index page. I dont want to wrap EVERYTHING in javascript, and EVERYTHING ELSE in tags. Unless there is an easy way of doing that.

What is the best way to load one page or the other depending on whether javascript is enabled or not?

+1  A: 

First, to directly answer your question, you could redirect the user away from the first index page if JavaScript is enabled using location.href. That is, if the user does not have JavaScript enabled, they will remain on page one. Else they will be redirected to the JavaScript version.

But the way I would generally go about doing this is to first make sure the website works without JavaScript; then add in the extra code for JavaScript after that plain version works.

<a href="foo.php" onclick="return foo();">Foo</a>

Here, if JS is not enabled, they will go to foo.php. Else, you can have the function foo do something and return false, thus keeping them on the page. The same thing can be done for forms.

konforce
A: 

Seems like putting a PHP redirect inside noscript tags would be the simplest way.

Swordgleam
`<noscript>` is client side; you cannot put any PHP code inside them... You could do something like `<noscript><a href="...">Click here for non-JS version</a></noscript>`, but doing it the other way around is more transparent.
konforce
Didn't quite think that one through. Thanks!
Swordgleam
+1  A: 
Gaby