Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check if an image file exists?

Status
Not open for further replies.

mjpost

Technical User
Feb 4, 2002
9
AU
I want to write a link only if a certain image exists in the target folder.

I need to use a client side script as this site will eventually end up as a CD-ROM based CBT.

So, anyone have a quick javascript that will check to see if a file exists?
 
One possible way is to use onload function for images. If image exists the onload events occurs. I used this method for our Product CD to check the new product version.
Look the following example:

<Script LANGUAGE=&quot;JavaScript&quot;>
function check(imgURL) {
var img=new Image();
img.onload=exists;
img.src=imgURL;
}
function exists() {
alert(&quot;exists&quot;);
}
</script>

PS. Opera does not support onload for image

Regards,
Sergey Smirnov
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top