Forum: Ruby on Rails How to detect JavaScript in RoR?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Johan (Guest)
on 2007-03-15 04:30
(Received via mailing list)
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
Bill W. (Guest)
on 2007-03-15 04:59
(Received via mailing list)
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
Tyler MacDonald (Guest)
on 2007-03-15 05:50
(Received via mailing list)
Johan <removed_email_address@domain.invalid> 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
Jeremy McAnally (Guest)
on 2007-03-15 05:54
(Received via mailing list)
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. <removed_email_address@domain.invalid> wrote:
>
> Best regards,
> Bill
>
>
>
> >
>


--
http://www.jeremymcanally.com/

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

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
Michael W. (Guest)
on 2007-03-15 06:08
(Received via mailing list)
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:

<script type="text/javascript">
   location.href = "controller/javascript_enabled_action";
</script>


--
Michael W.
Bill W. (Guest)
on 2007-03-15 14:55
(Received via mailing list)
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
Rob B. (Guest)
on 2007-03-15 15:25
(Received via mailing list)
On Mar 15, 2007, at 12:07 AM, Michael W. wrote:
>>
>> only returns true if that cookie is set.
> </script>
>
>
> --
> Michael W.

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

-Rob

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
Michael W. (Guest)
on 2007-03-15 15:35
(Received via mailing list)
Rob B. wrote:
>> </script>
> 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.
Jaroslaw Z. (Guest)
on 2007-03-15 16:21
(Received via mailing list)
On Mar 15, 2:29 am, "Johan" <removed_email_address@domain.invalid> 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.

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

--
Jarosław Zabiełło
http://blog.zabiello.com
Jeremy McAnally (Guest)
on 2007-03-15 17:28
(Received via mailing list)
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. <removed_email_address@domain.invalid> 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.mrneighborly.com/
http://www.rubyinpractice.com/
Peter De Berdt (Guest)
on 2007-03-15 17:38
(Received via mailing list)
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
Jeremy McAnally (Guest)
on 2007-03-15 17:46
(Received via mailing list)
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 <removed_email_address@domain.invalid> wrote:
>
>
>
> Best regards
>
>
>
>
> Peter De Berdt
>
>  >
>


--
http://www.jeremymcanally.com/

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

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
Bill W. (Guest)
on 2007-03-15 18:08
(Received via mailing list)
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
Peter De Berdt (Guest)
on 2007-03-15 18:24
(Received via mailing list)
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
This topic is locked and can not be replied to.