PDA

View Full Version : replace broken/missing images



regicidedelferoz
01-06-2012, 09:30 AM
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';

}

});

});

coothead
01-06-2012, 11:33 AM
Hi there regicidedelferoz,

I tested your first code in IE9, and it works OK. ;)

It will not work in IE8, and I assume IE6 and IE7 also, as those will browsers state that the height is 30px;

I would suggest that you try..


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

coothead

regicidedelferoz
01-07-2012, 01:37 AM
thanks for your reply sir,. but it seems that your code is not working fine sir,.

i am using firefox 9.0.1 and IE 9


but i already figure out the right code for jquery,.



$(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';

}

});

});



and i think this is the error



typeof this.naturalWidth == "undefined"