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

Johan [email protected] wrote:

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

Tyler MacDonald wrote:

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.

Checking for a cookie isn’t 100% reliable cause a user can have
JavaScript
enabled but cookies for that domain turned off. The standard way to do
this is
to do a redirect to another page if JavaScript is enabled/disabled such
as:


Michael W.

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

On 3/14/07, Bill W. [email protected] wrote:

Best regards,
Bill


http://www.jeremymcanally.com/

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:

http://www.rubyinpractice.com/

On Mar 15, 2007, at 12:07 AM, Michael W. wrote:

only returns true if that cookie is set.


Michael W.

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

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Rob B. wrote:

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

True, but not every app needs to track session information.


Michael W.

Hi Jeremy,

Jeremy McAnally wrote:

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

On Mar 15, 2:29 am, “Johan” [email protected] wrote:

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?

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

you don't have js


Jarosław Zabiełło
http://blog.zabiello.com

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

On 3/15/07, Bill W. [email protected] wrote:

I would like to render different views depending on if the client has
provides. I’m not sure what you see that doesn’t suggest the use of
respond_to, but would like to.

Thanks,
Bill


http://www.jeremymcanally.com/

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:

http://www.rubyinpractice.com/

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

–Jeremy

On 3/15/07, Peter De Berdt [email protected] wrote:

Best regards

Peter De Berdt


http://www.jeremymcanally.com/

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:

http://www.rubyinpractice.com/

On 15 Mar 2007, at 16:27, 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.

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

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

On 15 Mar 2007, at 17:07, Bill W. wrote:

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?

Best regards

Peter De Berdt