Simple IE 5.5 and IE 6 PNGFIX


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.