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