Check if file exists in Javascript

N

newcomer

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
 
L

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

Paul Cooper

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
 
R

Reply Via Newsgroup

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

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(fileToCheck)', 1000);
}
}

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

I hope something above helps steer you in the right direction,
randelld
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top