Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Testing for image presence

Reply
Thread Tools

Testing for image presence

 
 
Rick
Guest
Posts: n/a
 
      09-14-2006
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>

-------------------

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      09-14-2006
Rick said the following on 9/14/2006 3:19 AM:
> 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.


Upload all your files to a server and re-test it. You will get different
results.

> 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.


Only because the time it takes to parse the page is longer than the time
it takes the browser to get the image.

> -------------------
>
> <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>


You could put both of those script blocks into one and the effects are
easier to see. You will run into timing issues. If you want to ensure an
image is available then look into the onload event handler of the image
element and the onerror event handler.

<img src="someImage.jpg" onerror="this.src='someOtherImage.jpg'">

And you will still run into the potential problem of someOtherImage.jpg
not being available.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xmlrpclib, testing server presence rocco.rossi@gmail.com Python 1 02-02-2008 03:34 PM
Testing for the presence of input from stdin. Will McDonald Python 7 01-24-2006 10:39 AM
Testing for presence of arguments Madhusudan Singh Python 13 08-18-2005 04:40 PM
can i validate the presence of .Net framework in the OS a_srivathsan ASP .Net 2 09-08-2004 03:56 AM
strange routing table presence Hekaton Keires Cisco 0 01-27-2004 11:46 AM



Advertisments