Friday, July 10, 2009

IE8 JavaScript and backgroundImage

On one of my site I use a technique where I search for all elements that have the same title attribute. All the ones found I change the backgroundImage so those elements are highlighted.

Works brilliantly on all browsers (IE6, IE7, Firefox [all versions], Chrome, Safari, Opera and maybe a few others). But on IE8, it simply doesn't work!

Testing has shown that the JavaScript works, except where it changes the backgroundImage. That is where it [IE8] simply ignores the action requested of it.

Of interest, backgroundColor works. Just backgroundImage style property does not work.

I checked all the standards and found only one thing that seems to make a slight difference:

obj.style.backgroundImage = "url('images/myBgImage.gif')";

must become:

obj.style.backgroundImage = "url(images/myBgImage.gif)";

One must drop the single quotes.

At first this looked like we are going someplace. But now some work and others don't. Haven't noticed a pattern yet. So, I need to do some more work to figure out a solution to this very peculiar problem.

It is strange that Microsoft has not found this problem yet. There are a number of Blog entries that I have found when googling the problem. One of them gave me an idea which I shall be trying this weekend.

2 comments:

Unknown said...

i figured it out, you have to load the image first then it works. so in a hidden div load your image < src="yourimg" alt=""> then your javascript backgroundImage will display where you want it, Yay!! IE rocks :)

Wolfgang Wohlkinger said...

Thanks:-)

I had also by now figured out a solution but forgot to post my findings.

Essentially, IE8 does not support animated GIFs in CSS if you tile such an image as a background.

The solution was to enlarge the image file and remove any tiling (repeat in CSS). So in the end it was relatively simple once one knows this fact.