PNG transparency in IE/Win

Discussion in 'Web Design and Development (archive)' started by mnkeybsness, Dec 8, 2003.

  1. macrumors 68030

    mnkeybsness

    Joined:
    Jun 25, 2001
    Location:
    Moneyapolis, Minnesota
    #1
    After searching what seemed like forever, I found a little hack to make IE on Windows allow alpha transparency in PNG images.

    I'm sure someone will ask: "Why not just use a GIF?"...
    The answer is simple... take a look...
    [EDIT]Sample images have been lost[/EDIT]

    [edit] you should download the images and try placing a different color background on them than white. you will then notice that the GIF does not allow true alpha transparency and creates a white border around the image.[/edit]

    all you need to do to make this work is to add this little javascript to your page:
    Code:
    <!--[if gte IE 5.5000]>
    <script type="text/javascript" src="pngfix.js"></script>
    <![endif]-->
    
    add the following into a monospace text file and name it pngfix.js and place it in the root directory:
    Code:
    function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
       {
       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 
    		 var imgAttribs = img.attributes;
    		 for (var j=0; j<imgAttribs.length; j++)
    			{
    			var imgAttrib = imgAttribs[j];
    			if (imgAttrib.nodeName == "align")
    			   {		  
    			   if (imgAttrib.nodeValue == "left") imgStyle = "float:left;" + imgStyle
    			   if (imgAttrib.nodeValue == "right") imgStyle = "float:right;" + imgStyle
    			   break
    			   }
                }
    		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
    		 strNewHTML += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
    	     strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
    		 strNewHTML += "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
    		 img.outerHTML = strNewHTML
    		 i = i-1
    	     }
          }
       }
    window.attachEvent("onload", correctPNG);
     
  2. macrumors 6502a

    FattyMembrane

    Joined:
    Apr 14, 2002
    Location:
    bat country
    #2
    God bless you!:)

    the more web-design i do, the more i hate ie. even dillo can handle png files correctly.
     
  3. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #3
    Interesting...

    I'm going to have to try this at work tomorrow. :)
     
  4. macrumors 65816

    mrjamin

    Joined:
    Feb 6, 2003
    Location:
    Strongbadia
    #4
    I posted a similar find relatively recently.

    Here's the code i use:

    Code:
    // === SLEIGHT.JS CODE (Copyright© YoungPup) WITH MODIFIED CSS-INLINE IMAGE SUPPORT ========================
    if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
    	document.writeln('<style type="text/css">img { visibility:hidden; } </style>');
    	window.attachEvent("onload", fnLoadPngs);
    }
    
    function fnLoadPngs() {
    	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
    
    	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
    		if (itsAllGood && img.src.match(/\.png$/i) != null) {
    			var src = img.src;
    			img.style.width = img.width + "px";
    			img.style.height = img.height + "px";
    			img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
    			img.src = "images/spacer.gif"; // change this to your own gif
    		}
    		img.style.visibility = "visible";
    	}
    }
    
    if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
    	window.attachEvent("onload", alphaBackgrounds);
    }
    
    function alphaBackgrounds(){
    	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
    	for (i=0; i<document.all.length; i++){
    		var bg = document.all[i].currentStyle.backgroundImage;
    		if (itsAllGood && bg){
    			if (bg.match(/\.png/i) != null){
    				var mypng = bg.substring(5,bg.length-2);
    				document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
    				document.all[i].style.backgroundImage = "url('images/spacer.gif')";  // change this to your own gif
    			}
    		}
    	}
    }
    
    That also works for PNGs applied as background images (even in external stylesheets). All you have to do is change all mentions of 'images/spacer.gif' in the code above to the location of a 1px*1px transparent GIF.

    warning: if you use a DIV (for example) with a PNG24 background image, and that DIV has a hyperlink in it, the hyperlink will become unclickable since the JS works by overlaying the transparent GIF over the element with the PNG. Weird that; someone exploits a bug in IE which fixes another bug.
     
  5. macrumors newbie

    Joined:
    Oct 24, 2005
    #5
    PNG transparency in IE/Win

    Hello All
    If any body has solution to the bottom-most "warning" section of this posting, please let me know. Unfortunately I'm having the exact issue as mentioned there :(

    Thanks
    Sri
     

Share This Page