views:

1795

answers:

3

I want to spruce up some areas of my website with a few jQuery animations here and there, and I'm looking to replace my AJAX code entirely since my existing code is having some cross-browser compatibility issues. However, since jQuery is a JavaScript library, I'm worried about my pages not functioning correctly when JavaScript is turned off or doesn't exist in a user's browser.

I'll give an example: Currently, I'm using a pure CSS tooltip to give my users (players, the site is a browser game) information on other users. For example, if the other players in the game satisfy one or more conditions, a target icon is displayed next to their name, and upon hovering over that target icon information regarding the reasons behind the target is displayed. This is useful information, as it helps my players to know who they should plan to attack next in the game.

Currently, I do such tooltips using CSS. I have a parent div that holds the image of the target icon of class "info". I then have a div inside of that with class "tooltip" that, on the hover state of the "info" class that it is contained in, is shown, but on the normal state is hidden. I thought it was rather clever when I read about it, and since no JavaScript is used it works on any CSS compliant browser.

I would like to use jQuery to achieve the same effect, mostly because it would look much cleaner, but also because I believe quick and subtle animations can make such things "randomly appearing" make a lot more sense to the user, especially on the first encounter. I'm just wondering if the two will conflict. This is only one example of this, there are numerous other examples where the inability to use JavaScript would hinder the site.

So what I'm asking I guess is, how does one make a jQuery site degrade gracefully on browsers that do not support JavaScript, but otherwise do support most CSS? My goal is for the site to function on a basic level for all users, regardless of choice in browser. The animation is a good example, but I'm also worried about the more dynamic bits, like the auto-updating with AJAX, etc. Are there any good resources on how to achieve this, or do you have any advice about the best way such degradability could be achieved?

Thanks

PS: Totally irrelevant, but Firefox seems to think that "degradability" isn't a word, but "biodegradability" (with the "bio" prefix) is. Weird...

+5  A: 

If you consider the "Cascading Order" of css, could you not just add a css style at the very end of all your previous css definition in order to cancel any css effect you currently have for tooltip effect ?

That css rule would only be declared if Javascript is activated and JQuery detected.

That way, you are sure your css tooltip effect is not in conflict with your JQuery effect.

Something like:

a.info:hover span{ display:none}

with the use of "js_enabled" class to make this css rule conditional.

You also can do it by adding css rule on the fly:

function createCSSRule(rule,attributes)
{
    //Create the CSS rule
    var newRule = "\n"+rule+"{\n";
    for (var attribute in attributes)
    {
     newRule += "\t" + attribute + ": " + attributes[attribute] + ";\n";
    }
    newRule += "}\n";

    //Inject it in the style element or create a new one if it doesn't exist
    styleTag = $E('style[type="text/css"]') || new Element("style").setProperty('type','text/css').injectInside(document.head);
    if(window.ie)
    {
     styleTag.styleSheet.cssText += newRule;
    }
    else
    {
     styleTag.appendText(newRule);
    }
}


The most simple solution for Separation of CSS and Javascrip is to remove your css class

function jscss(a,o,c1,c2)
{
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): <-
      o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}

This example function takes four parameters:

a
defines the action you want the function to perform.
o
the object in question.
c1
the name of the first class
c2
the name of the second class

Possible actions are:

swap
replaces class c1 with class c2 in object o.
add
adds class c1 to the object o.
remove
removes class c1 from the object o.
check
test if class c1 is already applied to object o and returns true or false.

VonC
Another way of accomplishing the same thing is to add a class to the body tag with javascript (like class="js_enabled" or something), then override the styles rules for descendants of that class.
Matthew Crumley
Hmm... My current tooltip is using display: none; to hide the tooltip when its parent class is not hovered over. So ideally, I need to *remove* that line of CSS from the class if jQuery is working, which is a bit backwards from this example. Hmm indeed...
Nicholas Flynt
Oh, of course. A second class to override rules in the first. Why didn't I think of that before? (I commented too fast, just saw comment number one.)
Nicholas Flynt
Wow, the fix was actually pretty simple. I just replace the class entirely (using the removeClass and addClass functions) after the page loads for all my tooltips, and it works great. Thanks a bunch!
Nicholas Flynt
+1  A: 

If something can be done completely in CSS I say keep it that way. If lack of javascript in the browser is a concern, then most of the time I show the entire page unaffected.

Say for instance I'm going to use jQuery to toggle an element when a checkbox is clicked. On page load I look at the checkbox and update the element accordingly. If javascript is not enabled the element will still appear and the site will still be usable. Just not as nice.

smack0007
Right, I'm doing this with one control page on the site that shows a "preview" and disables buttons that can't be used, etc, and I'm disabling after page load, I'm familiar with this logic.
Nicholas Flynt
A: 

Man, you have a browser based game, right? You have less than 1% users with js disabled! And that 1% is the apocalyptic number, because i can BET that you have less than that ;)

Anyhow, if you are really concerned about this, just do the site without any javascript. And make it functional 100%. After your site works completely without any js flavour, just start to improve with jquery (or any other library;jquery is the best :P ). But with careful: do not change ANY of you html. It's easier than it looks ;)

And yes, if you have things that works without js (like those tooltips) keep it!

Ionut Staicu