2

Possible Duplicate:
How to detect if JavaScript is disabled?

I'm curious what other people would see as most viable for catching if a user has JavaScript disabled. Here's 2 types of methodology I'm thinking of using; if someone has a better idea or any ideas or things I should watch out for while putting either of these together please let me know.

First method

  1. pass a variable to PHP with a JavaScript function.
  2. depending on variable PHP either writes the JavaScript or non-JavaScript version.

Second method

  1. pass a variable to PHP with a JavaScript function.
  2. depending on variable PHP redirects using header to the JavaScript powered or non-JavaScript powered sites.

I'm guessing the first would be best seeing as the user wouldn't be redirected out of the page; however I'm curious is there an easier way to detect if JavaScript is disabled or enabled? Can I do that with a PHP function instead of using JavaScript. Using the above method would just assume that if they have JavaScript disabled the variable wouldn't be passed and the PHP would output the non-JavaScript version of the site.

Any takes?

Community
  • 1
  • 1
Brodie
  • 8,399
  • 8
  • 35
  • 55

4 Answers4

4

You don't need PHP to do this, it's all HTML.

Have the javascript page your main site. In the head of it, you can have a noscript tag, which inside it, has a meta refresh (or better) to your non-javascript page.

Jared
  • 396
  • 4
  • 14
3

Why not just use the noscript tag to display a message to the user? http://www.w3schools.com/tags/tag_noscript.asp

Unless you have more complex reason for seeing if javascript is active or not. Generally you just wrap a big message in the noscript tag indicating javascript is required and things won't work without it. Plain html, nothing fancy.

Brent Baisley
  • 12,641
  • 2
  • 26
  • 39
  • I wanted a separate site to print so they would be able to still navigate the site even witgout javascrpit. – Brodie Nov 05 '11 at 04:44
0

No need for the round trip. Should be able to tell whether or not the browser supports js on first request. There's an interesting discussion (and list of snippets) in the comments for get_browser (which seems like it might be overkill, and has some config requirements). Again, read past the API doc, and take a gander at the comments.

http://php.net/manual/en/function.get-browser.php

groundh0g
  • 408
  • 3
  • 9
0

Use a <noscript> tag and have a META redirect in that tag, so users without JavaScript go to your non-JS version.

So your code would look like this:

<noscript>
<meta http-equiv="refresh" content="0; url=http://foo.com/nojs">
</noscript>

Or, you could do something like this in your script tags:

location.href='http://foo.com/js';

This way, your main site is the non-js version, and if JavaScript is supported, it'll redirect to the JS-version using location.href

Some Guy
  • 15,854
  • 10
  • 58
  • 67