Ruby on Rails, Javascript detection
Asked Answered
B

3

11

I am creating a web app that uses Rails built-in helper functions to add ajax functionality to the site. I do not want the user to be able to use the app without JS as it will not function properly.

I need to be able to prevent this. How can I go about stopping the user from loading any of my pages without JS?

I have tried to use a tag in my layout. This detects the absence of JS but still renders the views. Putting the script tag around the yield prevents any view from being rendered.

Any ideas on how to do this?

Blum answered 13/4, 2009 at 22:22 Comment(0)
E
23

You can have a noscript block in the head element with a meta refresh tag in it. Example:

<head>
    <!-- title, scripts, css etc go here -->
    <noscript>
        <meta http-equiv="refresh" content="2;url=http://yoursite.com/nojswarning.html">
    </noscript>
</head>

you can read more about meta refresh here

Hope this helps

EDIT: To clarify, the noscript tag runs only if JavaScript is turned off for the user. So in this case if the JS is turned off the user will be redirected to http://yoursite.com/nojswarning.html. of course you can change this to be any page on any site you want.

Eggplant answered 13/4, 2009 at 22:47 Comment(1)
Worked great, thanks! For us, we created a javascriptdisabled.html file in the public/ directory that had the same styling as the 500.html error page. Then used <meta http-equiv="refresh" content="2;url=#{ root_url + "javascriptdisabled.html" }"> to dynamically generate the URL based on what environment you are in.Floret
P
2

The simple answer is to have the entire app load via JS. That way, the app would never show for someone who has JS enabled.

An alternative would be to start your page with an iframe, which loads to a JS-checking page. All the page does is redirect the user to the app's true URL via JS. Thus, if the user doesn't have JS, they won't get redirected (and you can inform them that they need JS with <noscript> tags). (I say to use an iframe so you don't give non-JS users an easy URL to get to the app with.)

Parallelism answered 13/4, 2009 at 22:47 Comment(0)
K
0

The <noscript></noscript> would seem to work. I would make the default a small loader with an overlay, and if the client does not support javascript, the javascript would never hide the loader. And the content in the <noscript> tag would show with the overlay.

Kilmer answered 14/4, 2009 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.