Click to See Complete Forum and Search --> : image array
rellik
02-22-2003, 09:19 AM
is it possible for JavaScript to replace empty img src's on the loading of a page so that you can leave multiple img tags blank and then have an image that says 'image unavailable' and then onLoad of page, replace the empty tag with the URl of this image?
say a function that searches the images array for empty src and then replaces them with unavailable.gif?
Charles
02-22-2003, 09:33 AM
Yes, but only if all of the images are of the same height and width. Try the following:
<script type="text/javascript">
<!--
for (var i=0; i<document.images.length; i++) document.images[i].onerror = function () {this.src = 'error.png'}
// -->
</script>
rellik
02-22-2003, 12:57 PM
cheers,
i have
<html>
<head>
<script type="text/javascript">
<!--
function checkImages() {
for (var i=0; i<document.images.length; i++)
document.images[i].onerror = function () {this.src = 'x.gif'}
}
//-->
</script>
</head>
<body onLoad="checkImages()">
<img src="" />
</body>
<html>
when the page loads it shows the red-cross as the image (unavailable, but when you rightclick>show picture, x.gif shows up. any ideas?
Charles
02-22-2003, 01:04 PM
As I pointed out, it's only supposed to work if all of the images have the same height and width. You example has a dummy image with a height and width of zero being replaced with a real image with a non-zero height and width.
rellik
02-22-2003, 01:11 PM
ok, so on what event of the IMG will work?
Charles
02-22-2003, 01:12 PM
Yes, Mr. Clark is right. My example should go at the bottom of the page and after all of the IMG elements. And it should not be called by the onunload handler. However, the size restriction on the images is going to be your big problem.
rellik
02-22-2003, 01:14 PM
ok, i get it. thank you both for your help!