How would you check if javascript is enabled in Rails? So that I could do something like this in the views:
<div>
<% if javascript_enabled? %>
<p>Javascript Enabled!</p>
<%- else -%>
<p>No Javascript</p>
<%- end -%>
</div>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can detect it, but it isn’t pretty.
First, you need a new controller with an action that updates a timeout in the session:
Next you need to include a javascript action in all your pages so this controller action is called on each page load. The easiest way is to include it on a “javascript-confirm.js” file included in your layout (on this particular example I used Prototype’s Ajax.Request, so you need to have it included on your javascripts, too):
This will invoke the confirm action in all your page views. Finally, you have to control how much time did it pass since your last confirmation in your application controller.
You will now have a variable called
@javascript_activein all your controllers.It should work even when the user activates/deactivates javascript, with a precision of 10 seconds. It might not work if some of your pages take longer than 10 pages to load (i.e. with lots of images). Increase the time limit in that case (on applicationcontroller and your javascript)
Disclaimer: I haven’t tested this code, some bugs might be lurking – but it should point you on the right direction.