Jump to content

Replace Broken/missing Images


regicidedelferoz

Recommended Posts

good day everyone, hi can anybody here wanna help me? i want to replace missing images automatically on load of the page,. i got some code on the net but it doesn't work on IE,. here's the code,.

for (var i=0; i<document.images.length; i++){        img = new Image();        img.src = document.images[i].src;        if (img.height == 0)            document.images[i].src = '<?php echo $livesitePath; ?>images/picnotavailable.jpg';    }

here's another one that is not workingon IE,.

$(window).load(function() {  $('img').each(function() {    if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0 || this.src=="") {	  // image was broken, replace with your new image	  this.src = '<?php echo $livesitePath; ?>images/picnotavailable.jpg';    }  });});

Link to comment
Share on other sites

are you trying to echo PHP from javascript? That won't work (IE or otherwise). By the time javascript is executed, PHP's window has long since passed. Perhaps you want to make an AJAX request or use a static link to fill in the gaps of missing images.

Link to comment
Share on other sites

are you trying to echo PHP from javascript? That won't work (IE or otherwise). By the time javascript is executed, PHP's window has long since passed. Perhaps you want to make an AJAX request or use a static link to fill in the gaps of missing images.
yes,. and it's working,.
Check for Javascript errors in IE. Print out the values you're checking (height, etc) to verify the values.
thanks for your reply,. got the right one,.
$(window).load(function() {  $('img').each(function() {    if (!this.complete || this.naturalWidth == 0 || this.src=="") {          // image was broken, replace with your new image          this.src = '<?php echo $livesitePath; ?>images/picnotavailable.jpg';    }  });});

this is the error i think

typeof this.naturalWidth == "undefined"

Link to comment
Share on other sites

yes,. and it's working,.
is the page itself already a PHP page? That was not immediately obvious from your post.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...