18

I am displaying a bunch of thumbnail images and the latency can be very high (over a VPN) so I send all the thumbnails in a single file (like a sprite) and set the CSS background-image and background-position properties of a div to show the thumbnails. The problem I'm having is with IE6 and figuring out when the image has loaded... I'm using the BackgroundImageCache hack:

document.execCommand("BackgroundImageCache",false,true);

To check when the image is loaded I use this code:

$('<img>').attr('src', 'ThumbSpriteTest.png').load(function() {
    $('.Thumbnails').css('background-image', 'url(ThumbSpriteTest.png)');
});

This works in every browser I've tried except IE6... even with the cache hack it is loading the image, firing the event, setting the background-image property and downloading the image again (and my .Thumbnail elements are blank while it re-downloads).

It seems to me that the cache hack is only changing the behavior of the CSS references and not the img tag. How can I tell when the background image is loaded without downloading it twice? Is it possible in IE6?

EDIT: Using: document.execCommand("BackgroundImageCache",true,true); seems to work (with both parameters as 'true'). I'm having trouble finding any documentation on the BackgroundImageCache command and it's parameters (I've found plenty of examples of using it to fix the CSS problem, but they all use false,true as parameters and don't explain them)... the bounty is still good for anyone with good information/documentation on the BackgroundImageCache command and it's parameters!

(I'm not sure why I'm excited to find something that works after wasting so many hours due to IE's shortcoming)

toby
  • 885
  • 3
  • 10
  • 21
  • 1
    Why do you want to load it twice (once as bg and once as src)? I would do it once with the element hidden out of view. One of my guesses on this behaviour would be a background vs image issue. But I can't be sure. Then again, we're talking about ghosts here **:D** – Christian Jun 08 '11 at 20:55
  • I don't want to load it twice, I just want to know when it's loaded and I don't think there's any way to know when the background-image has loaded. Other browsers cache the image and do not load it twice. – toby Jun 09 '11 at 14:12
  • 5
    You deserve a +1 just for being condemned to work with IE6. – zneak Jun 13 '11 at 13:25
  • 1
    Given that you've offered a bounty, I guess IE6 support is important to you. But it's worth re-stating that IE6 is a very old piece of software: No matter how many hacks and how good you jquery skills are, there are a lot of things which IE6 simply can't do or gets wrong. I don't know if this is one of them, but it seems likely. All I can do is suggest dropping support for IE6. If you've got a client who insists on it, tell them up-front that it will double the development cost and reduce the functionality. If they still insist on it, you'll just have to live with some things not working. – Spudley Jun 13 '11 at 13:32
  • Are you using that cache hack in conditional-comments so that it's only targeting IE6? You can also try to pop it in a try/catch block (`try { document.execCommand('BackgroundImageCache', false, true); } Catch (e) {};`) – stealthyninja Jun 13 '11 at 13:34
  • Could this be related to how jQuery handles these things? I know that jQuery has excellent cross browser support but have you tried the equivalent in native javascript? – 6twenty Jun 13 '11 at 14:05
  • "Condemned" or not, if you look at your stats and see 10% of your customers are still using IE6 then you're going to support that browser or face explaining a serious revenue hit to your boss! – Terence Johnson Jun 13 '11 at 14:16
  • 7
    I give you 150 bounty to let IE6 in the trash :) – Mohammed Swillam Jun 13 '11 at 15:29
  • I'd give all my reputation points to trash IE6 :) – toby Jun 13 '11 at 18:12
  • 1
    I dread to think what the selector `$(''` is doing - I would recommend using `$('img')` !!! – James Westgate Jun 14 '11 at 11:32
  • 2
    @TerenceJohnson There's a difference between supporting a browser and making it look good ;) Why bother making it look good. – Raynos Jun 14 '11 at 11:37
  • `$('')` creates a new image tag, I could just as easily have a hidden image tag and use that: `$('#MyHiddenImg')`. It does work in IE6, just doesn't look good, so it may end up that it just doesn't look good in IE6. – toby Jun 14 '11 at 13:23
  • @Spudley, the company I work for, an Internet retailer, has a significant percentage of revenue coming through IE6 visitors. Unfortunate, but reality. Sometimes there is not one client, but several tens of thousands of them. I do agree they can do without some functionality though, especially if it is only visual perks. – David Andersson Jun 16 '11 at 11:01

4 Answers4

3

This is definitely poorly documented, as it is considered a hotfix for ie6, and will stay that way, seeing this is already fixed in ie8. Anyway, here is what is dug up bout it.

execCommand method: http://msdn.microsoft.com/en-us/library/ms536419(v=vs.85).aspx

 bSuccess = object.execCommand(sCommand [, bUserInterface] [, vValue]);
 //sCommand is the name of command to execute
 //[bUse...] is to give permission to display a dialog window (if applicable)
 //[vValue] value to pass as parameter to the command

[bUserInterface]: is just a Boolean indicator for a dialog box, that is not used by all the possible command. But is used for example to save files / create link / etc... Eg: http://man.ddvip.com/web/dhtml/constants/createlink.html

So you may want to check if this value works when set to false, it should work in theory... But hotfixes can break for funny reasons.

About the hotfix: http://support.microsoft.com/kb/823727

Anyway, this feature only appear as a patch to IE6. So dun assume it will work for all ie6 browser. While it was introduced to prevent multiple loading + leakages, and not "caching" the way you are using it, it still does what the name suggests (hopefully). So dun be surprised it hiccups on the way on unpatched versions (auto update should fix this though)

With that warning, please catch the execution for the success or fail Boolean values, if you have features dependent on it. And I guess make the best with what you have (to be sad enough to be forced to support ie6)

PicoCreator
  • 9,886
  • 7
  • 43
  • 64
  • Now it's not making sense why changing the parameter to true would make any difference, but like you said it is a hotfix and may not be even be present. Also note that it is for IE6 SP1. Another indication it won't be present if the user isn't running Windows Update (probably unlikely, and I'm okay ignoring those users). – toby Jun 16 '11 at 13:25
  • @toby especially since it's a hot fix + using it for what it was not designed to use. Count itself lucky it works :) just dun count on it. It is tomes like these where u hate IE for trying to be special :x – PicoCreator Jun 16 '11 at 15:56
2

1) css property:

$('<img>').attr('src', 'ThumbSpriteTest.png').load(function() {
    $('.Thumbnails').css('background-image', 'url(ThumbSpriteTest.png)');
});

2) attr('src', 'ThumbSpriteTest.png') - may be a problem

The values of some attributes are reported inconsistently across browsers, and even across versions of a single browser. The .attr() method reduces such inconsistencies.

See http://api.jquery.com/attr/

3) Also:

<script type="text/javascript">
try {
 document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}
</script>

OR try CSS way

html { 
filter: expression(document.execCommand("BackgroundImageCache", false, true)); 
}

last examples were found here: Jquery IE6 hover problems, keeps loading background image

Community
  • 1
  • 1
Scherbius.com
  • 3,396
  • 4
  • 24
  • 44
  • I fixed the css attribute: 'url(ThumbSpriteTest)'. The jQuery functions are working properly, it's just the IE6 does not seem to care that the image has already been loaded in the img tag and reloads it for the css background-image attribute :( – toby Jun 13 '11 at 15:27
  • 1
    Check out this article: http://misterpixel.blogspot.com/2006/09/forensic-analysis-of-ie6.html and also I've added 3) to my answer – Scherbius.com Jun 13 '11 at 15:49
0

If you are using your code above for IE6 only and problem only reflects in IE6, then I guess your issue is the jquery... check the following:

$('.Thumbnails').css('background-image', 'url(ThumbSpriteTest.png)');

You have to add the 'url(img_src)'.

Marc Uberstein
  • 12,501
  • 3
  • 44
  • 72
0

you have to first insert into DOM, then attach to the img.load event, then put src and all should work in IE. The problem is because IE doesn't fire onload event always if src is set before the onload handler.

$('<img style="display:none"/>').appendTo('body').load(function() {
    $('.Thumbnails').css('background-image', 'url(ThumbSpriteTest.png)');
}).attr('src', 'ThumbSpriteTest.png');
venimus
  • 5,907
  • 2
  • 28
  • 38
  • It's not an issue with the event (the event is firing), it's an issue with the image getting downloaded twice. – toby Jun 15 '11 at 13:23
  • yes i get that, also saw that you managed to fix the problem, just wanted to point certain problems with ie and load event – venimus Jun 15 '11 at 15:24