How to fix png transparency problem in IE 5.5 and 6

In this programming tutorial we will learn how to fix png transparency problem in IE 5.5 and 6. In Internet Explorer 6 and below, transparent png images do not show accurately. The transparent part of the png do not look transparent but it gives a solid color look. Now Microsoft has introduced IE 9 but we should always keep this thing in our mind that the user of our website can also visit our website from IE 6 or prior than IE 6. So we have to fix this issue. Let's have a look over how to fix this issue.

How to fix png transparency problem in IE 5.5 and 6

You just have to insert the following code in the head tag of your web page.
<!--[if lt IE 7]>
<script language="JavaScript" type="text/javascript">
function fixPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}
window.attachEvent("onload", fixPNG);
</script>
<![endif]-->
So that's it. This is the way to fix the png transparency problem in IE 5.5 and 6.

This code will only execute if user's browser will be IE 6 or prior than IE 6. So there will be no extra load in your web browser if it is firefox, google chrome, opera, safari or internet explorer greater than 6.

I hope you will find this tutorial very informative.

I love your feedback.

0 comments: