By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

if file exist

P: n/a
how can i check if file exist in current directory (in javascript)?

--
pozdr;)
zhisol
Jul 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
In article <Xn************************@195.136.250.206>, zh****@o2.pl
enlightened us with...
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
-------------------------------------------------
Jul 20 '05 #2

P: n/a
kaeli <in********************@NOSPAMatt.net> wrote in
news:MP************************@nntp.lucent.com:
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:)
--
pozdr;)
zhisol
Jul 20 '05 #3

P: n/a
In article <Xn************************@195.136.250.206>, zh****@o2.pl
says...
how can i check if file exist in current directory (in javascript)?


Do you have server-side JavaScript or ASP with JavaScript facilities?
If you do, have a look at http://www.4guysfromrolla.com/
--
Hywel I do not eat quiche
http://hyweljenkins.co.uk/
http://hyweljenkins.co.uk/mfaq.php
Jul 20 '05 #4

P: n/a
zhisol <zh****@o2.pl> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
Lasse Reichstein Nielsen wrote:
if("cover.jpg" exist)
document.write('<img src='cover.jpg'>')
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.


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?

I need a way to discover the path of the running script's file (on the
server), read the directory said script resides in, and manipulate the
files there in a fairly simple way. This is all server side, of course.

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'm fairly new,
the above was painstakingly hacked out, and I've only got VB references
handy. Web searches aren't really helping.
Jul 20 '05 #6

P: n/a
Mark Space <ma**********@hotmail.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7

P: n/a
zhisol wrote:
[...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
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:)


Jul 20 '05 #8

P: n/a
Lasse Reichstein Nielsen wrote:
Mark Space <ma**********@hotmail.com> writes:
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.
Oh, right. It's been a while since I've done this, I missed the
difference between document.write and response.write.

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.
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.)
That depends on how the colFolders object is designed. If it is an array,
the simple version should work.
Just FYI, colFolders is a "collection", which is a built in data type in
VBScript. It's kinda like an associative array. It associates
key/value data pairs.

/L


Um, shouldn't that be "</L>"? ;)

Jul 20 '05 #9

P: n/a
Hi Mark,

Mark Space wrote:
Lasse Reichstein Nielsen wrote:

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.

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 ;-)
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #10

P: n/a
Stephen <ss*****@austin.rr.com> wrote in
news:oQ*******************@twister.austin.rr.com:
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 :(
http://jibbering.com/2002/4/httprequest.html

it seems, this web page doesn't exist

--
pozdr;)
zhisol
Jul 20 '05 #11

P: n/a
zhisol wrote:
Stephen <ss*****@austin.rr.com> wrote in
news:oQ*******************@twister.austin.rr.com:

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 :(


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
http://jibbering.com/2002/4/httprequest.html


it seems, this web page doesn't exist


Jul 20 '05 #12

P: n/a
zhisol wrote:
Stephen <ss*****@austin.rr.com> wrote in
news:oQ*******************@twister.austin.rr.com:

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 :(


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.
http://jibbering.com/2002/4/httprequest.html


it seems, this web page doesn't exist

Jul 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.