Click here to Skip to main content
6,630,289 members and growing! (16,072 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

PNG alpha support for Internet Explorer

By Partenon

An article on how to implement PNG alpha support for Internet Explorer
Javascript, Windows, Dev
Posted:15 Sep 2004
Views:47,918
Bookmarked:16 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 4.65 Rating: 4.47 out of 5

1

2

3
3 votes, 27.3%
4
8 votes, 72.7%
5

Introduction

Internet Explorer doesn�t support the alpha channel in PNG images. However the fix is to use the "AlphaImageLoader" filter. This script replaces all PNG images on the page with a blank image (blank.gif) and adds the "AlphaImageLoader" filter with the original image source. The blank image is to avoid the "broken image" picture to display above the filter. This script also supports printing and changing the source image at runtime.

Source

//Array containing all PNG images on the page

var PNGimageArray = new Array();
var isPrinting = false;

//Path to the blank image (1x1 transparent)

var blankSrc = "blank.gif";

//Captures print events

window.attachEvent("onbeforeprint", function () { beforePrint(); } );
window.attachEvent("onafterprint", function () { afterPrint(); } );
//Tests if element is a PNG image, and if so fixes it

function addPngImage(element){
  if (/\.png$/i.test(element.src)) {
    fixImage(element);
    element.attachEvent("onpropertychange", function () 
      { propertyChanged(); } );
    PNGimageArray[PNGimageArray.length] = element;
  }
}

//Applies filter and changes source to blank

function fixImage(element) {
  element.runtimeStyle.filter = 
   "progid:DXImageTransform.Microsoft.AlphaImageLoader(
      src='" + element.src + "')";
  element.src = blankSrc;
}

//If property "src" is changed fixs image (not 

//if it is changed to blank though)

function propertyChanged() {
  if (isPrinting) return;
  var element = event.srcElement;
  var pName = event.propertyName;
  if (pName != "src") return;
  if (!new RegExp(blankSrc).test(element.src))
    fixImage(element);

}

//Turns image back to original before print (Explorer can't print filters)

function beforePrint() {
  isPrinting = true;
  var element;
  for(var i = 0; i < PNGimageArray.length; i++){
    element = PNGimageArray[i];
    element.src = element.filters[0].src;
    element.runtimeStyle.filter = "";
  }

}

//Fixes image after print

function afterPrint() {
  isPrinting = false;
  var element;
  for(var i = 0; i < PNGimageArray.length; i++){
    element = PNGimageArray[i];
    fixImage(element);

  }
}
Style that makes all images run the addPngImage function. I use "expression" instead of "behavior" to avoid some security issues in Internet Explorer.
<style type="text/css"> img { filter:expression(addPngImage(this)); }
</style>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Partenon


Member
Software developer
Occupation: Web Developer
Location: Sweden Sweden

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralFIX for resizing images PinmemberMember 134512819:29 8 Dec '07  
GeneralWhat license? Pinmemberchappi14:55 20 Sep '06  
GeneralGreat Work But Resizing images doesnt work!!! PinmemberPier...2:28 29 Jun '06  
GeneralResource Protocol ! PinmemberNicolas MEURET23:49 13 Feb '06  
GeneralMaps PinmemberEDB_ENVC5:09 17 Jun '05  
GeneralRe: Maps Pinmembernoname98761:56 31 Jul '05  
GeneralFix for filenames with "(" or ")" PinmemberPolyVector13:03 17 Feb '05  
GeneralPNG Background images??? Pinmemberdclark15:26 11 Jan '05  
GeneralCross-Browser Compatibilty PinmemberDavy Boy12:53 29 Nov '04  
GeneralIt is a problem PinmemberSanjit Rath9:33 17 Oct '04  
GeneralBrilliant PinmemberCSnerd16:19 14 Oct '04  
GeneralRe: Brilliant PinmemberPartenon7:06 15 Oct '04  
GeneralLooks Familiar PinmemberDFU237:59 23 Sep '04  
GeneralRe: Looks Familiar PinmemberPartenon12:43 23 Sep '04  
GeneralWell known problem PinmemberPhilippe Lhoste1:15 22 Sep '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Sep 2004
Editor: Nishant Sivakumar
Copyright 2004 by Partenon
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project