views:

85

answers:

6

I don't want to deal with users who have javascript turned off, so I want to just prevent them from using my application. I want to detect if the user has javascript turned on so I can redirect them to a page that tells them they need to use javascript. I'm sure its in the request header. Anyone have any ideas how I can do this?

+3  A: 

Do you really need a redirect at all? Put the message in a <noscript> element, and it will only be displayed to those it applies to.

Rob Kennedy
+1  A: 

You only need html:

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

This of course won't prevent the server from serving content, it only will output this meta refresh tag, which will be parsed (and executed by redirecting the user) by clients that got no javascript or javascript disabled.

aularon
`noscript` cannot be in the `head` of the document. `meta` cannot be in the `body` of the document. It still works in some browsers, but this solution may considered invalid by some (like the all knowing W3C validator).
CD Sanchez
+5  A: 

The noscript tag is the easiest solution, though not exactly whay you're looking for.

<noscript>Get outta here.</noscript>
CD Sanchez
Thanks. You seem to be only one who answered who answers/asks about Rails.
DJTripleThreat
+3  A: 

You can't. The server has no idea what the client has enabled. You can't even "detect" if Javascript is disabled from the client (because what would you use to do so?).

You can use tricks to show content when Javascript is disabled (for instance, a <noscript> tag, or using Javascript to hide content that is meant for JS-only).

Daniel Vandersluis
+3  A: 

There is no way to detect JavaScript on the server side. However, you can use a NOSCRIPT tag to display a message indicating that your website requires JavaScript.

For an example, turn JavaScript off in your browser and load a Stack Overflow page. There'll be an obnoxious red message across the top of the page warning you that the site works best with JavaScript enabled. You can examine the Stack Overflow source code and CSS styling to see how they implemented it.

If you really want to deter non-JavaScript users from using your website, you could put your message in a div inside the noscript tag and expand the div to cover everything on the page.

Jeff
A: 

You could have a landing page with a snippet of javascript that runs and redirects to the real railsy landing page, or perform an AJAXY call to get the data you want to show the user. This would positively require javascript to be enabled to continue.