We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

bmayzure on 05/02/08


Tagged

fix ie png transparency


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

SmpleJohn
inamorix


Simple IE 5.5 and IE 6 PNGFIX


Published in: JavaScript 


URL: http://homepage.ntlworld.com/bobosola

I've been using this little gem of a snippet for a few years, and it's never failed me.

  1. /*
  2. Correctly handle PNG transparency in Win IE 5.5 & 6.
  3. http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.
  4.  
  5. Use in <HEAD> with DEFER keyword wrapped in conditional comments:
  6. <!--[if lt IE 7]>
  7. <script defer type="text/javascript" src="pngfix.js"></script>
  8. <![endif]-->
  9. */
  10.  
  11. var arVersion = navigator.appVersion.split("MSIE")
  12. var version = parseFloat(arVersion[1])
  13.  
  14. if ((version >= 5.5) && (document.body.filters))
  15. {
  16. for(var i=0; i<document.images.length; i++)
  17. {
  18. var img = document.images[i]
  19. var imgName = img.src.toUpperCase()
  20. if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
  21. {
  22. var imgID = (img.id) ? "id='" + img.id + "' " : ""
  23. var imgClass = (img.className) ? "class='" + img.className + "' " : ""
  24. var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
  25. var imgStyle = "display:inline-block;" + img.style.cssText
  26. if (img.align == "left") imgStyle = "float:left;" + imgStyle
  27. if (img.align == "right") imgStyle = "float:right;" + imgStyle
  28. if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
  29. var strNewHTML = "<span " + imgID + imgClass + imgTitle
  30. + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
  31. + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
  32. + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
  33. img.outerHTML = strNewHTML
  34. i = i-1
  35. }
  36. }
  37. }

Report this snippet 

You need to login to post a comment.