Welcome to WebmasterWorld Guest from 54.196.232.162

Forum Moderators: incrediBILL

Message Too Old, No Replies

Replace red boxes

     
12:53 pm on Apr 17, 2008 (gmt 0)

Preferred Member from GB 

10+ Year Member

joined:Jan 26, 2004
posts:636
votes: 0


Anyone got any ideas where i can find a script that if an image is unavaliable that it gets replaced with a default image?
2:29 pm on Apr 17, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 25, 2002
posts:3185
votes: 0


Hi stevelibby,

There is an onError attribute that can be used to detect if the image doesn't load. I'm not sure how supported this is though!


<img src="link/to/image.gif" alt="Alt Text" onError="imageError(this)" />

<script type="text/javascript">
function imageError(element) {
element.style.border = '1px solid red';
}
</script>

In principle this should work, but I've not tested it!

HTH

[edited by: BlobFisk at 2:30 pm (utc) on April 17, 2008]

3:01 pm on Apr 17, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 25, 2002
posts:3185
votes: 0


Just did a quick test and that's IE only.
3:07 pm on Apr 17, 2008 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


You could use javascript from the body's onLoad event and loop through images, checking their size. If the image has no width or height (i.e. it never loaded), then change the src property of the image to the default one.
5:18 pm on Apr 17, 2008 (gmt 0)

Full Member

5+ Year Member

joined:Dec 3, 2006
posts:257
votes: 0


Just did a quick test and that's IE only.

It works in FF too (and Opera, and Safari).