Not registered yet?
Register now! It is easy and done in 1 minute and gives you access to special discounts and much more!
By Lance Pollard
<%- else -%>
<%- end -%>
If that's all you want it to do, it might be best to put the non-JS version in the view:
This approach should be usable for practically any progressive enhancement you want to add.
<link rel="icon" href="favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
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:
// Assuming you are using Prototype
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
Disclaimer: I haven't tested this code, some bugs might be lurking - but it should point you on the right direction.
You can't detect it on the server-side. Indeed, scripting might be turned off the first time your page is accessed, then the browser settings changed and re-navigated without a new HTTP request being made. Not to mention all sorts of caching issues and places where scripting might be technically ‘enabled’ but not working due to conflicts, bugs or ‘security’ tools getting in the way.
document.getElementById('isitjs').innerHTML= '<p>Sparkly script-enhanced content!<\/p>';
// or DOM methods, as you prefer
Put this in your application_helper.rb
This goes in your view:
That's not possible during rendering of the response. The only way to figure that less or more reliable is to let the client send an ajaxical request beforehand in one of the previous requests. This ajaxical request should then set some token in the server side session which identifies that the client has sent an ajaxical request beforehand (which thus proves that the client has JS enabled). But that doesn't cover the possibility that the client can disable JS in meanwhile during a session. Or you must hassle with collecting timestamps and ajaxical requests after every response. Not really something you'd like to do.
Rather render the both contents and make use of <noscript> and <script> tags to toggle the one or other. Also see my answer here for several examples: opposite of <noscript>.