Jump to content

Replace Broken/missing Images


regicidedelferoz
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...