Click to See Complete Forum and Search --> : A script for missing images


Perfidus
01-07-2004, 10:26 AM
I wonder if there's a solution to substitute missing images with a default image, something that introduces a default picture when "image src" is pointing to an image that is not in the specified folder anymore

96turnerri
01-07-2004, 10:35 AM
<img src="xx.jpg" onError="document.images[0].src='yy.jpg';" onAbort="document.images[0].src='yy.jog';">

change xx.jpg for picture you wish to use

[0] to say [3] if this image is third image onpage

onError is for when img not found
onAbort when a user cancel page load before completion

96

Pittimann
01-07-2004, 11:03 AM
Hi!

Just putting the code like this will lead to an alert because of stack overflow in IE. If you want to avoid that, you could use:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function replaceImg(){
onError=document.images[0].src='yy.jpg';
onAbort=document.images[0].src='yy.jpg';
}
//-->
</script>
</head>
<body onload="replaceImg()">
<img src="xx.jpg">
</body>
</html>

Cheers - Pit

Btw - images[3] is for the 4th image.

96turnerri
01-07-2004, 11:21 AM
i know lol typo

Perfidus
01-07-2004, 12:06 PM
But, you talk about the order of appearance of the image in the page [1], [2]
But what happens when the content is generated dinamically?

http://costa4seasons.com/dutch/fichapiso.html?Ref=Ben5006

If you go here you will see what I'm talking about

96turnerri
01-07-2004, 12:45 PM
i see you want a generic one same for all pages try either code not using images[0] using images or images[all] not sure if that will work but i know theres a way true them for now ill do some research

96

Perfidus
01-07-2004, 03:21 PM
I have tried those:
images[]
images
images[all]

but they do not work, any hints?

96turnerri
01-07-2004, 06:31 PM
<img src="xx.jpg" onError="document.images[0].src='yy.jpg'; document.images[1].src='yy.jpg'; document.images[2].src='yy.jpg'; document.images[3].src='yy.jpg';" onAbort="document.images[0].src='yy.jpg'; document.images[1].src='yy.jpg'; document.images[2].src='yy.jpg'; document.images[3].src='yy.jpg'">

96turnerri
01-07-2004, 06:32 PM
sorry this

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function replaceImg(){
onError=document.images[0].src='yy.jpg'; document.images[1].src='yy.jpg'; document.images[2].src='yy.jpg'; document.images[3].src='yy.jpg';
onAbort=document.images[0].src='yy.jpg'; document.images[1].src='yy.jpg'; document.images[2].src='yy.jpg'; document.images[3].src='yy.jpg';
}
//-->
</script>
</head>
<body onload="replaceImg()">
<img src="xx.jpg">
</body>
</html>

Perfidus
01-07-2004, 08:59 PM
This code doesn't work neither, I think it should be something more general that can load an image on error or on abort.

96turnerri
01-08-2004, 06:41 AM
yes it does i jst checked it

96turnerri
01-08-2004, 06:46 AM
o i c what u have done, what did i say about change images use this

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function replaceImg(){
onError=document.images[27].src='neutro.jpg'; document.images[28].src='neutro.jpg'; document.images[29].src='neutro.jpg'; document.images[30].src='neutro.jpg';
onAbort=document.images[27].src='neutro.jpg'; document.images[28].src='neutro.jpg'; document.images[29].src='neutro.jpg'; document.images[30].src='neutro.jpg';
}
//-->
</script>
</head>
<body onload="resizeWindow(); replaceImg();">
Hi
</body>
</html>

onload as the two functions combined not two seperate atrributes and change [0] to number of pictures you wish to change!!!!!!!!

Perfidus
01-08-2004, 10:24 PM
OK, lets do the question in a different way.
My document is generated by php so it is impossible to know how many images it will have cause they depend on database. I wonder if there is a way to automatically substitute whatever image in website with an alternative graphic if there's an error loading the picture.

Pittimann
01-09-2004, 08:28 AM
Hi!

Sorry guys - I messed up this thread with my post!

The stack overflow was just coming, because the image to replace the non existing one didn't exist as well. Due to that, I had to get this error.

Your very first proposal, 96turnerri, is absolutely ok, provided that the image "yy.gif" exists. So, Perfidus, just let your PHP always add the onError= and onAbort= like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<img src="xx1.gif" onError="this.src='yy.gif'" onAbort="this.src='yy.gif'">
<img src="xx2.gif" onError="this.src='yy.gif'" onAbort="this.src='yy.gif'">
<img src="xx3.gif" onError="this.src='yy.gif'" onAbort="this.src='yy.gif'">
<img src="xx4.gif" onError="this.src='yy.gif'" onAbort="this.src='yy.gif'">
</body>
</html>

SHAME ON ME and sorry once again! :rolleyes:

Cheers - Pit