473,480 Members | 2,194 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Testing for image presence

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>

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

Sep 14 '06 #1
1 1654
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/
Sep 14 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2279
by: Andrew | last post by:
Hi, I am thinking about ways for efficient techniques for isolation testing. Here is the problem as I see it: Complex OO systems are designed with massive number of dependencies between modules...
16
2645
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
14
11015
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
1
2671
by: Veetold | last post by:
The solution you suggested works very good - thanks again. I encountered a new problem related to this topic. According to your advise I put the following line to print and format event: ...
5
2715
by: Brian | last post by:
Hello all.. Am working on an Air Hockey game... have an table loaded into a picture box. The borders of the table are slightly slanted. Am using hit testing lines with GDI+ to manipulate the...
2
1363
by: spgmbl | last post by:
I have set up the local environment to use sqlserver mode testing. The article i followed to install was here: http://support.microsoft.com/default.aspx?scid=kb;en-us;317604 I also changed the...
3
7709
by: | last post by:
I'm using the DataList and GridView controls, and I am trying to wrap my head around the problem of conditionally showing or hiding cells/cell content based on the presence or absence of DB data. I...
23
5856
by: Peter | last post by:
I have a problem with a page show_image.asp that returns a jpg image under Windows XP Pro SP2. The page sets content type as: Response.ContentType = "image/jpg" While this works perfectly fine...
1
1065
by: rocco.rossi | last post by:
I'm employing xmlrpclib for a project at work, and I must say that I'm quite impressed with its effectiveness and ease of use. However, I was recently doing some tests when I realized that if the...
0
7054
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3000
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.