Welcome to Atalasoft Community Sign in | Join | Help

PNG Transparency in Internet Explorer Revisited

There have been countless times where I have made user interfaces that were easily customizable using style sheets.  Usually the graphics were generic enough so that you could change the colors without making it look too bad.  Then there are the interfaces with the drop shadows, overlays, and other semi-transparent areas which would cause headaches when trying to mimic in IE.

Recently I was tasked with creating a slick looking AJAX demo app, and I do so much development in FireFox, I had forgotten that IE still doesn't handle the alpha channel well.  I had already created most of the graphics using png's with alpha transparency, and I didn't want to remake everything.  So I Googled around the net looking for a simple solution, but most solutions required using extra event handlers for each image tag's onload, or something similar.  I'm somewhat lazy, and I don't want to have to add an attribute to all my image tags. 

I tinkered around a bit, and found a relatively easy way to get it to work.  The only thing you have to do is make sure that your png image tags have the width and height attributes defined.

Here's a solution I came up with:

window.onload = OnPageLoad;

function OnPageLoad(){
   if (document.all)
      fixPngs();
}

// Loops through all img tags
function fixPngs(){
   for (i = 0; i < document.images.length; i++){
      var s = document.images[i].src;
      if (s.indexOf('.png') > 0)  // Checks for the .png extension
         fixPng(s, document.images[i]);
   }
}

// u = url of the image
// o = image object
function fixPng(u, o){
   o.src = 'spacer.gif';  // Need to give it an image so we don't get the red x
   o.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + u + "', sizingMethod='scale')";
}
Published Wednesday, May 10, 2006 4:09 PM by David Cilley

Comments

Wednesday, July 05, 2006 3:24 PM by RobsPages

# re: PNG Transparency in Internet Explorer Revisited

Dave -

Neat script! It saved me a ton a work and as I am lazy too, thats a good thing.

I made 2 mods though. First off, I use the ajaxManager framework from dhtmlNervana.com which looks like this:

<code>
function newAjaxObject(){
if (document.getElementById) {
var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}
}

function ajaxManager(){
var args = ajaxManager.arguments;
 if (document.getElementById) {
var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}

switch (args[0]){
case "load_page":
if (x){
x.onreadystatechange = function(){
if (x.readyState == 4 && x.status == 200){
el = document.getElementById(args[2]);
el.innerHTML = x.responseText;
if (document.all){
fixPngs();
}
}
}
x.open("GET", args[1], true);
x.send(null);
}
break;

case "start_up":
ajaxManager('load_page', 'welcome.html', 'mainDisplay');
ajaxManager('load_page', '/includes/nav.php?s=mainNav', 'nav');
ajaxManager('load_page', '/includes/nav.php?s=footer', 'footer');
   break;
 
case "sub_nav":
var underScore = args[1].indexOf("_");
var subMenu = args[1].substr(0, underScore);
var menuSelect = "/includes/nav.php?s=mainNav&sm=" + subMenu;
ajaxManager('load_page', args[1], 'mainDisplay');
ajaxManager('load_page', menuSelect, 'nav');
el = document.getElementById('debug');
el.innerHTML = menuSelect;
break;

} //END SWITCH
} //END SELECT

</code>

As you can see, I slipped your code into the 'load_page' case which runs the function whenever a new piece of content is loaded - but only includes it once.

Thanks again for the code.

--Rob
Thursday, August 03, 2006 11:59 AM by David Cilley's Web Imaging Blog

# AJAX Thumbnails and On Demand Tiling Unleashed!

It's been a while since I last spoke about anything on this blog... the main reason for this is we've...
Thursday, January 11, 2007 3:00 PM by David Cilley's Web Imaging Blog

# AJAX Thumbnails and On Demand Tiling Unleashed!

It's been a while since I last spoke about anything on this blog... the main reason for this is we've

Anonymous comments are disabled