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?
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?
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).
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?
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.
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..."
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.
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.
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?