if file exist

K

kaeli

how can i check if file exist in current directory (in javascript)?

This isn't really possible with normal JS in a normal security
environment.

Did you mean on the server or on the client?
Is this for a web page or an intranet application, CD-ROM, etc...?


-------------------------------------------------
~kaeli~
Hey, if you got it flaunt it! If you don't, stare
at someone who does. Just don't lick the TV screen,
it leaves streaks.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
 
Z

zhisol

This isn't really possible with normal JS in a normal security
environment.

Did you mean on the server or on the client?
Is this for a web page or an intranet application, CD-ROM, etc...?

it's for web page. i mean exactly that:

if("cover.jpg" exist)
document.write('<img src='cover.jpg'>')
else
//display nothing

and i don't know how can i realise "if("cover.jpg" exist)"

for example i have address: http://mysite.com/show.html
and if in root on server is a file "cover.jpg", make this visable,
otherwise not.

normally in html if "cover.jpg" doesn't exist - client see empty box, but
i want that he'll see nothing

ps: sorry for my english:)
 
L

Lasse Reichstein Nielsen

zhisol said:
if("cover.jpg" exist)
document.write('<img src='cover.jpg'>')
else
//display nothing

This will not work, since checking whether it exists will be
asynchroneous. You will not be able to wait for the result before
writing the image tag.
and i don't know how can i realise "if("cover.jpg" exist)"

What you can do is write the image tag with a src pointing to an empty
image, and then change the src if cover.jpg exists.

function setIfExists(id,src) {
var img = new Image();
img.onload = function () {
document.images[id].src = src;
};
img.onerror = function () {
// do something if images doesn't exist.
}
img.src = src;
}

for example i have address: http://mysite.com/show.html
and if in root on server is a file "cover.jpg", make this visable,
otherwise not.

normally in html if "cover.jpg" doesn't exist - client see empty box, but
i want that he'll see nothing

You can also start the image out hidden (either visibility:hidden or
display:none) and change it to something visible if the image exists.

/L
 
L

Lasse Reichstein Nielsen

Mark Space said:
Huh? As it happens, I was going to ask almost exactly the same
question. Your answer makes no sense to me. I mean, in vbscript I
can read files, directories, etc. But it doesn't work in JavaScript?
Wow. You don't mean client side vs. server side, do you?

Yes. As I read the question (which can be incorrect, it is not very
precisely stated), the original poster wants to check whether an image
exists in the root of the server, and if so, insert an img tag with
document.write. You can only use document.write on the client, so
the check must be performed on the client and check the existence of
a file on the server. For that, my answer is correct.

If you are talking server side scripting, which is probably ASP then,
I would recommend an ASP-specific group, since it is not really about
Javascript.
In vbscript I can do something like this:

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
pathXlat = Request.ServerVariables("PATH_TRANSLATED")
pathXlatRoot = Left( pathXlat, InStrRev( pathXlat, "\" ) )
Response.Write "My Path: " & pathXlatRoot & "index.html <br>"
and the path shows up just fine. I can also step through the files or
folders in my subdirectory like so:

Set objFolder = objFSO.GetFolder( pathXlatRoot )
Set colFolders = objFolder.SubFolders
For Each objSubFolder in colFolders
If FileExists( objSubFolder, "a_name" ) Then
call do_something()
End If
Next

No problemo. Anyone know the javascript equivalent?

I'll assume that the ASP objects work the same. If that is true, then
the following should work in JScript:

var objFSO = Server.CreateObject("Scripting.FileSystemObject");
var pathXlat = Request.ServerVariables("PATH_TRANSLATED");
var pathXlatRoot = pathXlat.substring(0,pathXlat.lastIndexOf("/")+1);
Response.Write("My Path: " + pathXlatRoot + "index.html <br>");

and

var objFolder = objFSO.GetFolder(pathXlatRoot);
var colFolders = objFolder.subFolders;
for (var index in colFolders) {
var objSubFolder = colFolders[index];
if (FileExists(objSubFolder, "a_name")) {
do_something();
}
}

This would be the equivelent JScript code.
Maybe the FileExists function isn't global in JScript. I would expect
it to be a method of the file system object.
Maybe the "for(...in...)" will include too many properties, and
it would be better to do
for (var index = 0; index < colFolders.length ; i++) {
That depends on how the colFolders object is designed. If it is an array,
the simple version should work.

/L
 
S

Stephen

zhisol said:
[...snip...]

it's for web page. i mean exactly that:

if("cover.jpg" exist)
document.write('<img src='cover.jpg'>')
else
//display nothing

and i don't know how can i realise "if("cover.jpg" exist)"

The HTTPRequest object could be useful to see if file is there or not.
Combine this with the idea of starting out showing an "empty" image or
maybe an invisible image.

Send a HEAD request for the "cover.jpg" file. In the handler that is
triggered when the response comes back, check for 200 response code &
then make the image visible at that point.
See:
http://jibbering.com/2002/4/httprequest.html
HTH
Stephen
 
L

Laurent Bugnion, GalaSoft

Hi Mark,

Mark said:
That was actually my next question. Which is better for server side
scripting, VBScript or Javascript? It seemed to me that Javascript
actually had more standards that recognized it, and I wondered if it was
more popular than MS only VBScript. I've only done VBScript so far.
(CGI like Perl or PHP really aren't an option for this project.)

VBScript is probably more used than JavaScript on ASP. I don't know the
actual figures though. Most examples you will find on the Web for ASP
development are also written in VBScript.

Which doesn't matter much, actually, since the API is exactly the same
for VBScript and JScript (just like the API in .NET is exactly the same
if you programe in VB.NET or C#, or any other supported language).

For this reason, I rather recommend using JScript on ASP. It has the
great advantage to allow you to use the same language, same syntax for
server-side and client-side scripting. Besides, and though it's a
controversial point, the JScript syntax is C-like, and IMHO makes more
sense than the VB syntax. If you know JScript, you will feel comfortable
in C, C++, Java, C#. If you know VB, you'll feel comfortable in... VB ;-)

Of course it's a personal choice in the end. Technically, there are no
differences. It's more a question of gut feeling, and of what languages
you already know, and plan to use in the future.

HTH,

Laurent
(currently involved in a .NET project in VB.NET and C#, and constantly
reminded that C# is nicer ;-)
 
Z

zhisol

Send a HEAD request for the "cover.jpg" file. In the handler that is
triggered when the response comes back, check for 200 response code &
then make the image visible at that point.

<script language="JavaScript">
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("HEAD", "cover.jpg", false);
xmlHttp.send();
document.write(xmlHttp.statusText);
</script>

is that correct to see return code on the browser screen?
i see nothing :(
it seems, this web page doesn't exist
 
S

Stephen

zhisol said:
<script language="JavaScript">
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("HEAD", "cover.jpg", false);
xmlHttp.send();
document.write(xmlHttp.statusText);
</script>

is that correct to see return code on the browser screen?
i see nothing :(

Should work. Note:

xmlHttp.status should give, e.g., "200"
xmlHttp.statusText should give the associated text, e.g., "OK"

This works for me in my environment, just as you coded it above (except
for substituting an image name I know is on my system).

Server must be configured to allow HEAD requests. Most probably are, but
you'll have to make sure in your case.

You probably also want to check for other possible response codes that
indicate the requested entity is present, e.g., 304.

Regards,
Stephen
 
S

Stephen

zhisol said:
<script language="JavaScript">
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("HEAD", "cover.jpg", false);
xmlHttp.send();
document.write(xmlHttp.statusText);
</script>

is that correct to see return code on the browser screen?
i see nothing :(

Should work. Note:

xmlHttp.status should give, e.g., "200"
xmlHttp.statusText should give the associated text, e.g., "OK"

This works for me in my environment, just as you coded it above (except
for substituting an image name I know is on my system).

Server must be configured to allow HEAD requests. Most probably are, but
you'll have to make sure in your case.

You probably also want to check for other possible response codes that
indicate the requested entity is present, e.g., 304.

Regards,
Stephen

P.S.: need document.close() after the document.write(...)?? Or try
alert instead of document.write()...?
S.
 

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

No members online now.

Forum statistics

Threads
474,093
Messages
2,570,607
Members
47,226
Latest member
uatas12

Latest Threads

Top