473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 34724
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Szlazak | last post by:
All files that I will be referring to are in the same file folder. I need my javascipt file to write or save the contents of a textarea to other local files. This is for local use only on one...
3
by: Matt | last post by:
The user can select a file in the file open dialog using <input type="FILE">, but the user can also enter the full file path in the browse text box manually. My question is if there are JavaScript...
3
by: Matt | last post by:
Is there a way to use JavaScript to check if a file path exists? For example, user enter C:\eklrjlejr in a text box, and I want to check if the file really exists. Is there a way to use JavaScript...
3
by: Matt | last post by:
<input type="file" size=50"> will produce the browse button and browse text box. The user can either select the file from browse button, or enter a path in browse text box manually. My question...
1
by: Matt | last post by:
There are no client-side javascript to check if the file path exists because the security reason. Correct? Because we don't want code to access our local machine from remote machine, that's...
10
by: Ricardo Luceac | last post by:
Hi all. I'm having a problem with this, I have look if a file exists, if don't wait till it is created and if it exists I need to open it. I do the following: for (; ; ) {
14
by: John Salerno | last post by:
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a...
3
by: trint | last post by:
How can I do this with my c# code with my website(because the file is there, but the code doesn't return it)?: if(File.Exists(String.Format("~/images/categories/{0}", sFileName)) return...
3
by: pollygw | last post by:
I have a page that dynamically adds rows to a table and the user can also delete any of the rows in no specific order. When the form is submitted I need to do some validation. I can't loop through...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.