How to detect JavaScript in RoR?

Hi,

This must be basic, but I'm unable to find the answer on the net. I would like to render different views depending on if the client has JavaScript enabled or not. Is there any easy/standard way for Rails to find out if JavaScript is enabled, so that I can test for this in my controller object?

Thanks, Johan

Hi Johan, Johan wrote:

I would like to render different views depending on if the client has JavaScript enabled or not. Is there any easy/standard way for Rails to find out if JavaScript is enabled, so that I can test for this in my controller object?

Yes. See the respond_to method at http://api.rubyonrails.org/. Let us know if you need help.

Best regards, Bill

I haven't had to do this yet, but if I did, I think I'd do this:

  Write a piece of javascript that, on page load, sends a request to a special method.

  Configure your layour to only render that javascript if your cookie hasn't already been set.

  Write a method in your application controller like "has_javascript?" that only returns true if that cookie is set.

    - Tyler

I don't think respond_to is quite what he's looking for.

I'm not sure that there is an easy way to do this, since clients are client and your controller is on the server (and as far as I know, there can't be any direct communication between them without some sort of roundabout solution).

--Jeremy

Tyler MacDonald wrote:

Hi Jeremy,

Jeremy McAnally wrote:

I don't think respond_to is quite what he's looking for.

Hi Johan, Johan wrote: > I would like to render different views depending on if the client has > JavaScript enabled or not. Is there any easy/standard way for Rails to > find out if JavaScript is enabled, so that I can test for this in my > controller object?

Yes. See the respond_to method at http://api.rubyonrails.org/. Let us know if you need help.

Could you say more? Rendering different views depending on whether or not the request is for html or js (or other types) is exactly what respond_to provides. I'm not sure what you see that doesn't suggest the use of respond_to, but would like to.

Thanks, Bill

But if cookies are turned off, your application is headed for trouble with sessions anyway.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

Rob Biedenharn wrote:

It has nothing to do with RoR. Use HTML tag: noscript.

<html>   <script>alert('you have js');</script>   <noscript>you don't have js</noscript> </html>

He's asking to see whether the client has JavaScript enabled (" depending on if the client has JavaScript enabled or not"); that is not what respond_to does. The respond_to method is for rendering different views based on the request type: HTML for HTML, JavaScript for Javascript (RJS), XML for XHR. It does not do _anything_ on the client side.

From the docs: "If the client wants HTML, we just redirect them back to the person list. If they want Javascript (wants.js), then it is an RJS request and we render the RJS template associated with this action. Lastly, if the client wants XML, we render the created person as XML..."

--Jeremy

Exactly, the only way to determine if a user has JS enabled or not I know of, is by using JS to set a certain variable (e.g. a cookie) on your landing page (which should be compatible with both JS and non-JS browsers). You can then test for this variable on subsequent requests.

Best regards

Peter De Berdt

Yeahp, which basically isn't any different than what you'd with PHP or something else as far as I know.

--Jeremy

Hi Jeremy, Jeremy McAnally wrote:

He's asking to see whether the client has JavaScript enabled ("depending on if the client has JavaScript enabled or not"); that is not what respond_to does. The respond_to method is for rendering different views based on the request type: HTML for HTML, JavaScript for Javascript (RJS), XML for XHR. It does not do _anything_ on the client side.

Ah. I see the nuance you're getting at. And I'd agree that there's no way, Rails or otherwise, to render a different index page depending on whether or not the client has JS enabled. OTOH, if the links and/or buttons on the first view are rendered using link_remote_to or (potentially empty) form_remote_tag, respond_to is very useful for rendering different views subsequent to the index page. I'm using it quite successfully for that.

Best regards, Bill

Graceful degradation (by specifying an :action with form_remote and a :href with link_remote) for non-JS browsers should always be the ultimate goal. On the other hand, if you are sure your userbase will have JS enabled (for example a custom solution for use within your company), you can put more time in extra features than making sure your site nicely degrades.

Making entirely different views for JS and non-JS users doesn’t feel right to me to be honest, you’re doubling your code and probably will abuse AJAX instead of thinking over when to use it and when you shouldn’t. A good example would be dropping back button functionality and bookmarkability of your pages. This will happen if you use AJAX for everything, and unless you’re putting a huge amount of time in implementing something like StateManager, you’ll alienate your users. Why do you think so many Flash developers have put so much time in finding workarounds for these nuisances?

Best regards

Peter De Berdt