Forum: Rails Spinoffs (closed, excessive spam) Browser javascript support -- how to detect?

Posted by djlewis (Guest)
on 2008-06-29 05:45
(Received via mailing list)
Is there a simple way to tell if the browser you are talking to
supports javascript and has it turned on? I'd like to set a variable
to that effect.

Thanks. --David.
Posted by T.J. Crowder (Guest)
on 2008-06-29 13:41
(Received via mailing list)
David,

Ummm....if it doesn't support JavaScript, what kind of "variable" are
you talking about?  Server-side?  'Cause if there is no client-side
JavaScript, there are no client-side JavaScript variables.  Or perhaps
you mean a cookie?

You can definitely test whether JavaScript is enabled:  Have the page
assume that it isn't, and then use JavaScript to change that
assumption if it is.  The two main variations on this theme, off the
top of my head, are:

1. Have a page that's fully-functional without JavaScript, but gets
"spruced up" by JavaScript if it's enabled.  No "detection" per se is
required, it's just that the sprucing up won't happen if the client
doesn't have JavaScript.

2. Have a small intro page that redirects to a version of your page
that doesn't use JavaScript if JavaScript isn't enabled, or to a
version that does if it is.  You can easily do that by having
JavaScript trigger a page load (by setting location.href) immediately,
and having a meta refresh fallback that happens after a pause (e.g.,
if the JavaScript thing hasn't happened).

In either case, if you like you can set a cookie to remember whether
JavaScript is loaded for subsequent page loads (have the page set the
cookie to "not enabled", and set it to "enabled" via JavaScript in the
page), although if the browser supports JavaScript the user can change
whether it's enabled between page loads, so your cookie can get out of
date.  Users don't typically do that, of course.

Hope this helps,
--
T.J. Crowder
tj / crowder software / com
Posted by RobG (Guest)
on 2008-06-30 08:26
(Received via mailing list)
On Jun 29, 9:40 pm, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> David,
>
> Ummm....if it doesn't support JavaScript, what kind of "variable" are
> you talking about?  Server-side?  'Cause if there is no client-side
> JavaScript, there are no client-side JavaScript variables.  Or perhaps
> you mean a cookie?

Given the rise in use of extensions like NoScript, it is likely that
even if some of the scripts in the page work, others won't if they
load from a different URI.  For example, I frequently allow scripts
from the site I'm visiting but don't allow Urchin tracker or google
analytics.


> You can definitely test whether JavaScript is enabled:  Have the page
> assume that it isn't, and then use JavaScript to change that
> assumption if it is.  The two main variations on this theme, off the
> top of my head, are:

That will probably work a good percentage of the time but it isn't
foolproof.  It may also be that the detection method itself failed.


> 1. Have a page that's fully-functional without JavaScript, but gets
> "spruced up" by JavaScript if it's enabled.  No "detection" per se is
> required, it's just that the sprucing up won't happen if the client
> doesn't have JavaScript.

Or if the javascript fails to execute properly.


> 2. Have a small intro page that redirects to a version of your page
> that doesn't use JavaScript if JavaScript isn't enabled, or to a
> version that does if it is.  You can easily do that by having
> JavaScript trigger a page load (by setting location.href) immediately,
> and having a meta refresh fallback that happens after a pause (e.g.,
> if the JavaScript thing hasn't happened).

Unless the meta refresh has been disabled too.


> In either case, if you like you can set a cookie to remember whether
> JavaScript is loaded for subsequent page loads (have the page set the
> cookie to "not enabled", and set it to "enabled" via JavaScript in the
> page), although if the browser supports JavaScript the user can change
> whether it's enabled between page loads, so your cookie can get out of
> date.  Users don't typically do that, of course.

They do, using extensions like NoScript, and they may also disable
cookies too.  The usual strategy for web sites is to make them
functional without scripting, then tart them up with script.  Some
sites also give visitors subtle hints that scripting would allow them
better functionality.


--
Rob
Posted by djlewis (Guest)
on 2008-07-01 16:16
(Received via mailing list)
Thanks for the replies.

So, I take it there is no ~simple~ way to detect javascript-ness, like
looking at user_agent.  Anything involves some trick or another, and
requires some interaction with attempted js that "fails".

I was kinda hoping that Prototype or Script.aculo.us had a function
built-in that gave you the answer.  I just want to set a session
variable so I know not even to bother with js interactions.

I realize the holy grail is gracefully degrading (or incrementally
enhanced) interaction, but my reading (and experimenting) so far
indicates that is not always easy. Besides, I'm just getting started
with js on rails, and wanted to experiment a bit.

--David.
Posted by Frederick Polgardy (Guest)
on 2008-07-01 17:00
(Received via mailing list)
How could Prototype or Scriptaculous tell you whether JavaScript is
enabled?!  They're JavaScript libraries!

Am I misunderstanding something?

-Fred

On Tue, Jul 1, 2008 at 9:16 AM, djlewis <djlewis9@gmail.com> wrote:

>
> I was kinda hoping that Prototype or Script.aculo.us had a function
> built-in that gave you the answer.  I just want to set a session
> variable so I know not even to bother with js interactions.


--
Science answers questions; philosophy questions answers.
This topic is locked and can not be replied to.