Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails, Javascript detection

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?

like image 748
stellard Avatar asked Apr 13 '09 22:04

stellard


People also ask

Can you use JavaScript with Ruby on Rails?

JavaScript is a very popular programming language used in lots of projects, also those in Ruby on Rails.

What is Rails Ujs?

Rails UJS (Unobtrusive JavaScript) is the JavaScript library that helps Rails do its magic when we use options like remote: true for many of the html helpers. In this article I'll try to explain the main concept of how this works to make it transparent for the user.

How do I add AJAX in Rails?

In Rails, submitting an AJAX request can be done as easily as adding remote: true to a link, button, or form. From there you can have any response be some JavaScript code waiting on the server side, and it will execute in the client's browser. Here's the simplest code example of UJS via AJAX in a link.


1 Answers

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.

like image 127
Darko Z Avatar answered Oct 03 '22 07:10

Darko Z