R
Rick
I consider myself a pretty good javascript coder, but I'm far from guru
status and would appreciate feedback from some more experienced people.
I came up with this idea for conditionally outputting an image based on
whether it is available or not (code below) and tested it. It appears
to work, but I'm concerned that there may be something fundamentally
flawed in this as I've never seen this done before nor have any of my
Google seraches turned up anyone recommending anything like this.
So, any feedback about this would be appreciated.
In the test below, "testimage.gif" does not exist, but testimage2.jpg
does exist. When you run this, it outputs the default.jpg instead of
testimage.gif and testimage2.jpg shows up as expected.
-------------------
<html>
<head><title>test</title>
<script languge="javascript">
myImage = new Image();
myImage.src = "testimage.gif"; // this does not exist
myImage2 = new Image();
myImage2.src = "testimage2.jpg"; // this does exist
</script>
</head>
<body>
myImage<br>
<script language="javascript">
if ( (myImage.complete) && (myImage.height) && (myImage.width) )
{
document.write("<img src=\""+myImage.src+"\">");
}
else
{
document.write("<img src=\"default.jpg\">");
}
</script>
<br><br><br>
myImage2<br>
<script language="javascript">
if ( (myImage2.complete) && (myImage2.height) && (myImage2.width) )
{
document.write("<img src=\""+myImage2.src+"\">");
}
else
{
document.write("<img src=\"default.jpg\">");
}
</script>
</body>
</html>
-------------------
status and would appreciate feedback from some more experienced people.
I came up with this idea for conditionally outputting an image based on
whether it is available or not (code below) and tested it. It appears
to work, but I'm concerned that there may be something fundamentally
flawed in this as I've never seen this done before nor have any of my
Google seraches turned up anyone recommending anything like this.
So, any feedback about this would be appreciated.
In the test below, "testimage.gif" does not exist, but testimage2.jpg
does exist. When you run this, it outputs the default.jpg instead of
testimage.gif and testimage2.jpg shows up as expected.
-------------------
<html>
<head><title>test</title>
<script languge="javascript">
myImage = new Image();
myImage.src = "testimage.gif"; // this does not exist
myImage2 = new Image();
myImage2.src = "testimage2.jpg"; // this does exist
</script>
</head>
<body>
myImage<br>
<script language="javascript">
if ( (myImage.complete) && (myImage.height) && (myImage.width) )
{
document.write("<img src=\""+myImage.src+"\">");
}
else
{
document.write("<img src=\"default.jpg\">");
}
</script>
<br><br><br>
myImage2<br>
<script language="javascript">
if ( (myImage2.complete) && (myImage2.height) && (myImage2.width) )
{
document.write("<img src=\""+myImage2.src+"\">");
}
else
{
document.write("<img src=\"default.jpg\">");
}
</script>
</body>
</html>
-------------------