Storing javascript preferences

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
sime's picture

As part of SWF Tools you can embed Flash into the webpage in 2 obvious ways:
- directly embedded into the html.
- with a flash replacement technique (eg. SWFObject, jquery.flash.js, and UFO to come)

So, say my choice, as administrator, is SWFObject. But what if the user has javascript turned off? I think the result is no flash (even though the use might want to see it).

My understanding is that the webpage could use AJAX to call back to the server, and tell it that Javascript is OK. Once Drupal knows this, then we can comfortably use Flash replacement. Another use-case is if we know the user has Firefox, why use flash-replacement technique in this case, we can safely drop it and embed the flash normally!

Anyone looked at something like this? Maybe aware of this feature in, say, jstools? I guess it would consist of:
- AJAX callback
- storage of setting on profile or session
- Manual setting UI

Thanks
Simon

Comments

It would be useful

nedjo's picture

in many contexts to be able to respond on the server side differently depending if javascript is enabled or not.

I'll have a go at drafting an approach.

Looking for to your ideas.

sime's picture

Looking for to your ideas.

Posted a quick module

nedjo's picture

in CVS as jsenabled. Please try it out and if it's what you need I'll open a project for it. This would be good for other uses like not sending fallback content to the browser, saving bandwidth, e.g., in a use like Activeselect.

Here's the README:

This helper module can be used by other modules to track Javascript support
and so serve up different content accordingly.

It sets a $_SESSION['jsenabled'] variable. There are three possible values:

  • JSENABLED_UNKNOWN (-1): Javascript support is not yet determined.
  • JSENABLED_FALSE (0): Javascript support is known to be absent.
  • JSENABLED_TRUE (1): Javascript support is known to be present.

The function jsenabled_status() returns the current support status (one
of these three constants).

So you can do something like:

<?php
switch (jsenabled_status()) {
  case
JSENABLED_UNKNOWN:
  case
JSENABLED_FALSE:
   
// Provide content with non-Javascript fallbacks.
   
break;
  case
JSENABLED_TRUE:
   
// Provide content for Javascript browsers.
   
break;
}
?>

Caution should be taken as it is possible that users will change their
browser's settings mid-session and hence go from supporting Javascript
to not, or vice versa.

Great!

sime's picture

Very simple and nice. I will get into some tests this week with swftools.

Do you think that other checks would follow a similar design pattern? I'm thinking browser version, flash, svg, etc.

Good question...

nedjo's picture

Some of this can be detected without a javascript callback. Browser version is in the request from the browser. Flash detection has to be done on a custom basis on the client, see e.g. http://www.quirksmode.org/js/flash.html. (Presumably detecting Flash or SVG would depend on js being enabled.)

Maybe the module should be 'browser support'. That said, I'm not likely to take on adding any of these other pieces any time soon.

On top of js checking, I am

sime's picture

On top of js checking, I am also thinking of adding flash checking to swftools. So my thoughts are whether to strive for a central module, or just duplicate your design in my module.

Anyway, thanks heaps for these ideas. I will have a play and post back here if I make any progress.

D6 inclusion ?

yched's picture

In my 'batch / progressive' patch for core (http://drupal.org/node/127539) where I had a similar need, I used a simple 'has-js' cookie set by drupal.js. But the cookie does not get a chance to be set until the user visits a js-enriched page.

Your solution seems much more reliable, in that it actively 'forces' detection. Do you think you could turn your module into a D6 patch ? I think a core jsenabled_status() function would be useful in many places.

nedjo

sime's picture

nedjo

This works great for me :) Thanks!!
As discussed, I think it would be cool to turn it into a "browser detection" module, keeping the code modular of course.

I won't be adding much anytime soon, but in the project description I would probably state the following goals:
-- javascript checking
-- browser version
-- flash version/check
-- svg
-- quicktime
... and so on.

I also thought that some user.module integration would be cool eventually. Perhaps allowing the user to request no javascript/flash, regardless of what the system could discover about their environment.

Anyroads. Just confirm that you are happy for me to make a project out of this, and also whether you also want cvs access.

@yched: I think if this were ever suitable for core, it would be like token and require some time in contrib.

Simon

I think if this were ever

yched's picture

I think if this were ever suitable for core, it would be like token and require some time in contrib.

Maybe so. Token is still in contrib essentially because Dries is not (yet) convinced of its generic qualities, whereas, with the batch patch having landed last week, D6 HEAD already has a js-enabled detection feature - only not as neat as yours...

yes true/interesting. nedjo

sime's picture

yes true/interesting. nedjo would be the one for it though, I wouldn't trust myself on this.

/me runs away to the safety of contrib.

overview of current and or past discussion...

kasperjames-gdo's picture

being new to the entire world of drupal im curious as to how people can write perfect text and code, however, i am a bit curious as to how you can misspell common phrases and words.

anyways, off the subject...

i keep seeing code. obviously js, and others.

command lines for things like the one inside the "drupal upgrade (from 4.7 to 5.x) video" on lullubot or whatever that is.

anyways, how do i get that "dos" like window to work? is this something called mimi or around that kinda termo'?

this might be the wrong place to ask things, and everyones so big on class order and stuff, but it seems that the current topic users are onto some good level grounding.

whats up with scripting? is everyone that interested in it that much, theyll open projects and just take a hands on approach that fast? i can and do appreciate it, obviously...becuase i use this, and your knowledge helps me get to know more.

anyways...blah blah blah sorry my language isnt that advanced yet here.

again with the questions...whats the best mod for including js? html?
and when i run into errors....again, since i dont have the knowledge of how to manually
edit like the above users appear to be able to do...whare can i place these codes and/or
find the place or strings to make adjustments and accomidations to the end result im looking
for?

thanks,

KasperJames
www.kasperjames.com/