Search code examples
javascriptruby-on-railsserver-sideerbnoscript

Check if Javascript is Enabled (Serverside) with Rails


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>

Solution

  • 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:

    class JavascriptController < ApplicationController
      def confirm
        session[:javascript_updated] = Time.now
      end
    end
    

    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):

    function confirmJavascript()
    {
        // Assuming you are using Prototype
        new Ajax.Request('/JavascriptController/confirm');
    }
    
    myTimeoutFunction();
    setInterval(myTimeoutFunction, 10000); // invoke each 10 seconds
    

    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.

    class ApplicationController < ActionController::Base
    
    JAVASCRIPT_TIME_LIMIT = 10.seconds
    
    before_filter :prepare_javascript_test
    
    private
    def prepare_javascript_test
      if (session[:javascript_updated].blank? or
         Time.now - session[:javascript_updated] > ApplicationController::JAVASCRIPT_TIME_LIMIT)
        @javascript_active = true
      else
        @javascript_active = false
      end
    end
    
    end
    

    You will now have a variable called @javascript_active in 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.