Raya Deleva’s Web

/ Web development

The PNG problem in Windows Internet Explorer

For the Web, PNG really has three main advantages over GIF:

  • alpha channels (variable transparency),
  • cross-platform gamma correction (control of image brightness) and color correction
  • two-dimensional interlacing (a method of progressive display).

PNG also compresses better than GIF in almost every case (5% to 25% in typical cases).

Here is a JavaScript-based PNG fix for Internet Explorer 5.5 and 6 on Windows. The fix allows IE to properly render PNG alpha transparency.


<!--[if lt IE 7]>
<script language="JavaScript">
function correctPNG() // 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", correctPNG);
</script>
<![endif]-->

visit: homepage.ntlworld.com/bobosola/index.htm
category: Web development, posted date: 28.06.2006, Comment

Add your comment here.

Your name*

Your email

Your comments, suggestions, compliments or maybe other?*


What is this blog about? - A blog about sharing wisdoms mostly connected with web development. I truly hope that you will find something useful here. Cheers, Raya.
» About
» Playlist (Social music experiment)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 Subscribe

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CSS Beauty A List Apart W3schools The best forum for development in Bulgarian CSS Zen Garden Photo Cult PEAR - PHP Extension and Application Repository Rapid development framework for PHP
My lovely friend Bobbyta Awesome! - Mootools Smashing Magazine