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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Johan <johan.mayumi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Walton <bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> > 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 > > > > > >-- http://www.jeremymcanally.com/ My free Ruby e-book: http://www.humblelittlerubybook.com/book/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tyler MacDonald wrote:> Johan <johan.mayumi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.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 Wang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Jeremy, Jeremy McAnally wrote:> I don''t think respond_to is quite what he''s looking for. > > On 3/14/07, Bill Walton <bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote: >> >> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 15, 2007, at 12:07 AM, Michael Wang wrote:> Tyler MacDonald wrote: >> Johan <johan.mayumi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > 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 WangBut if cookies are turned off, your application is headed for trouble with sessions anyway. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Rob Biedenharn wrote:> On Mar 15, 2007, at 12:07 AM, Michael Wang wrote: >> 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>> 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 Wang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 15, 2:29 am, "Johan" <johan.may...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Walton <bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> > Hi Jeremy, > > Jeremy McAnally wrote: > > I don''t think respond_to is quite what he''s looking for. > > > > On 3/14/07, Bill Walton <bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote: > >> > >> 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 > > > > > >-- http://www.jeremymcanally.com/ My free Ruby e-book: http://www.humblelittlerubybook.com/book/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <peter.de.berdt-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org> wrote:> > > 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 > > > >-- http://www.jeremymcanally.com/ My free Ruby e-book: http://www.humblelittlerubybook.com/book/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 15 Mar 2007, at 17:07, Bill Walton wrote:> 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? Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---