470,618 Members | 1,786 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,618 developers. It's quick & easy.

Check if file exists in Javascript

Is there a way to check if a file exists in Javascript?
This is what I'm trying to do:
if(thisfile.htm exists)
do this
else
do that
Jul 20 '05 #1
3 34526
Lee
newcomer said:

Is there a way to check if a file exists in Javascript?
This is what I'm trying to do:
if(thisfile.htm exists)
do this
else
do that


Whether or not you can do something
in Javascript
in Javascript on a server
in Javascript in a Windows Scripting Host shell
in Javascript in all commonly used browsers
or
in Javascript in a particular version of a particular browser

Are all different questions. Which do you mean?

Jul 20 '05 #2
On 4 Mar 2004 07:59:00 -0800, mi**************@hotmail.com (newcomer)
wrote:
Is there a way to check if a file exists in Javascript?
This is what I'm trying to do:
if(thisfile.htm exists)
do this
else
do that


Client side, no there isn't under the standard security model. AFAIK
it can be done with Internet Explorer provided the user allows an
ActiveX control to run, but I certainly wouldn't!

Paul
Jul 20 '05 #3
newcomer wrote:
Is there a way to check if a file exists in Javascript?
This is what I'm trying to do:
if(thisfile.htm exists)
do this
else
do that


I do not believe it is possible - however you could cheat - there is a
method to preload an image without actually displaying it - and one can,
after the image has downloaded, check its size before displaying it.

Thus... one could try and test for an html file as opposed to an image
file - Give it a few seconds wait before checking the size of the html
file and if its greater than zero, the page is likely to exist.

I say likely in such that you might find that sometimes the page is
being read from a proxy cache somewhere. One method around this might
be to append arguements to your requested html file, like a time stamp.
Thus, request whatever.html?13451324345 - This *might* mean that your
request comes from the original server as opposed to a copy that might
exist on a users proxy.

There are alot of mights there - I'm a newbie with javascript, but I
come from a Unix/Linux background which provides many solutions to a
single problem - Its thought me to think this way with javascript.

The following code is untested - again - I'm a newbie so someone else
might be able to clean it up a little/lot...

function checkIfRemoteFileExists(fileToCheck)
{
var tmp=new Image;
tmp.src=fileToCheck;
if(tmp.complete)
{ window.alert(fileToCheck+" is available"); }
else
{ // Not got it yet - retry for a few seconds...
window.setTimeout('checkIfRemoteFileExists(fileToC heck)', 1000);
}
}

checkIfRemoteFileExists('http://www.where-ever.com/abc.html');

I hope something above helps steer you in the right direction,
randelld
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark Szlazak | last post: by
10 posts views Thread by Ricardo Luceac | last post: by
14 posts views Thread by John Salerno | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.