Posted By

jackkeller on 11/01/11


Tagged


Versions (?)

pngfix


 / Published in: JavaScript
 

  1. //best overall method for automatically detecting PNG's in a document
  2. var strGif = "/images/clear.gif";
  3.  
  4. function correctPNG() {
  5. for (var i = 0; i < document.images.length; i++) {
  6. var img = document.images[i];
  7. var imgName = img.src.toUpperCase();
  8. if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
  9. var imgID = (img.id) ? "id='" + img.id + "' " : "";
  10. var imgClass = (img.className) ? "class='" + img.className + "' " : "";
  11. var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
  12. var imgStyle = "display:inline-block;" + img.style.cssText;
  13. if (img.align == "left") imgStyle = "float:left;" + imgStyle;
  14. if (img.align == "right") imgStyle = "float:right;" + imgStyle;
  15. if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
  16. if (img.useMap) {
  17. strAddMap = "<br/><img style=\"position:relative; top:-" + img.height + "px;" + "height:" + img.height + "px;width:" + img.width + "\" " + "src=\"" + strGif + "\" usemap=\"" + img.useMap + "\" border=\"" + img.border + "\">";
  18. }
  19. var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
  20. if (img.useMap) strNewHTML += strAddMap;
  21. img.outerHTML = strNewHTML;
  22. i = i - 1;
  23. }
  24. }
  25. }
  26.  
  27. window.attachEvent("onload", correctPNG);

Report this snippet  

You need to login to post a comment.