Problem with transparent PNGs in IE

Translation of the original in Catalan. Read it in: CatalàCastellano

Continuing with the subject of transparencies and .png files, well it turns out that the title of the portal, which used to be a .gif, is now a .png, also with the transparency needed so that the background colour of the page shows through. What happens is that this .png no longer looks right in IE, as always for incomprehensible M$ reasons. So Law, the creator of the title image, has passed me a small javascript that solves the problem.

First of all what we have to modify is where the image is called, for example:

<img src="path/imatge_transparent.png" onLoad="fixPNG(this)"  />

The fixPNG function specifically has this JavaScript code:

function fixPNG(myImage){
	var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	var imgTitle = (myImage.title) ? "title='" + myImage.title + "' " : "title='" + myImage.alt + "' "
	var imgStyle = "display:inline-block;" + myImage.style.cssText
	var strNewHTML = "<span " + imgID + imgClass + imgTitle
	strNewHTML += " style=\"" + "width:" + myImage.width + "px; height:" + myImage.height + "px;" + imgStyle + ";"
	strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	strNewHTML += "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
	myImage.outerHTML = strNewHTML
}

If we don't want to leave this code in the middle of the HTML/xHTML code, we can save this function, for example, in the titol.js file and then embed it in the meta-code like this:

<script type='text/javascript' src='path/titol.js'></script>

Thanks a lot, LAW!

2 comments

Comments from the old blog, as they were written. Comments are closed.

  1. —

    Un problema derivat de l'anterior és de les transparències en els CSS i que es comenta a la web de la Chavalina, concretament a aquest post: Transparencias CSS. Molt útil com a referència en problemàtiques d'aquest tipus.

  2. Chavalina

    <p>Muchas gracias por la referencia, espero que os haya sido útil.</p>