Forum Moderators: open

Message Too Old, No Replies

Image Error

Can Javascript detect it and display accordingly?

         

rover

7:58 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Does anyone know if there is a way to use javascript (or any other way for that matter) so that if an html page is trying to display an image, and there is an error, that it would then place a default image, or display nothing?

For example if the code was:

<img border="0" src="image_name.gif" width="125" height="84">

and there was no file "image_name.gif" present, it would then display a default image (pixel.gif), or it would display nothing, rather than showing an error?

adni18

9:49 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aha! I'm glad you asked! I recently came up with a script to do this very thing:

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
teryaki=new Array();
function doneloaded(low)
{
teryaki=new Array(teryaki, low);
}
function checkLoaders()
{
calvary=document.getElementsByTagName("img");
var h=0;
for(var b=0;b<calvary.length;b++)
{
if(calvary[b].className=="person_thumbnail")
{
for(var c=0;c<teryaki.length;c++)
{
if(teryaki[c]==calvary[b])
{
h=1;
}
}
if(h==0)
{
calvary[b].src="error.jpg";
}
}
}
}
//-->
</SCRIPT>

On each <IMG> tag, add: onLoad="doneloaded(this)"

and on the <BODY> tag, add: onLoad="checkLoaders()"

rover

10:40 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Wow - made to order. Thanks very much! I just tested it and it worked fine. (I just removed the part where it had the class="person_thumbnail"). So the end code for me was:

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
teryaki=new Array();
function doneloaded(low)
{
teryaki=new Array(teryaki, low);
}
function checkLoaders()
{
calvary=document.getElementsByTagName("img");
var h=0;
for(var b=0;b<calvary.length;b++)
{
for(var c=0;c<teryaki.length;c++)
{
if(teryaki[c]==calvary[b])
{
h=1;
}
}
if(h==0)
{
calvary[b].src="error.gif";
}
}
}

//-->
</SCRIPT>

adni18

12:25 am on Jan 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



teryaki=new Array(teryaki, low);

Nothing like adding flavor to your variable names! :-D