Drupal Behavior to Fix IE6 CSS Background Image Flickering

When the new myLifetime community launched we wanted to focus on performance, and one trick was to minimize HTTP requests by using the CSS sprites technique. For a great run down of this technique and examples of other major sites using this, check out this resource on Smashing Magazine.

Double checking this implementation with YSlow! everything was working great.

Well almost. Then we tried IE6. I hate IE6.

Then I discovered this excellent post detailing IE6 and its BackgroundImageCache usage.

After trying that JS trick, we noticed the page was significantly faster & smoother in IE6. With heavy CSS sprite usage this was a necessary fix. And with future sites in the works with this same technique, seems like a reoccurring fix.

To Drupal-ize & jQuery-ize this fix for resuability, I wrote this simple behavior below that works with Drupal 6. You can see in action on the myLifetime community.

  1. /**
  2.  * Fix flickering background images in IE.
  3.  */
  4. Drupal.behaviors.fixIEFlickr = function() {
  5.   if (jQuery.browser.msie) {
  6.     try {
  7.       document.execCommand('BackgroundImageCache', false, true);
  8.     } catch(err) {}
  9.   }
  10. };

May this code save you a few hours/days of head banging!

Hi Ted, I was curious

Hi Ted, I was curious regarding the fact that you didn’t use CSS/JS aggregation for your CSS/JS files, is it because you use a CDN (ltcdn.com) ? Thansk for sharing this tip.

@Tostinni aggregation is

@Tostinni aggregation is usually on but sometimes the other engineers turn it off to debug some other caching issues with the CDN. No normal reason to have it off :) But the CDN does help greatly even if it is off.

Thanks Ted, in fact I was

Thanks Ted, in fact I was suprised, by the speed of the site, in Opera display is blazzing fast so it wasn’t an issue having the CSS/JS files split. Bests

Hi Ted, This solution

Hi Ted,

This solution sounds brilliant and just what I’m looking for!

I only have one problem….as a newbee in Drupal, I haven’t got any idea WHERE to put this code….. can you help me out?.... what file needs to be changed? Do I create a new module?

I would love to add a background-image to my site, but not if it flickers in IE!!

Thanks in advance,

Linda

Linda a few background

Linda a few background images should still work fine in IE6. If you are using a lot of them like in the above linked examples that is where you can run into issues.

To use this technique create a new JavaScript file and use your themes info file to include this script. You could also optionally add this in a module using drupal_add_js but that might be more work than is needed.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Textile markup to format text.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <img> <pre>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options