Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File exists function

Status
Not open for further replies.

draigGoch

Programmer
Feb 10, 2005
166
GB
Can anyone tell me if there is a file exists method in javascript that I can use.

Example pseudo:

if myFile.exists
{
alert ("hello")
}

Thanks in advance

A computer always does what you tell it to, but rarely does what you want it to.....
 
Yes - but it is IE-only, as it relies on ActiveX:

Code:
var fsObj = new ActiveXObject('Scripting.FileSystemObject');
if (fso.FileExists('c:\\someFile.txt')) {
   alert('Yes!');
}

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 


Oops - the "fso." in the second line should read "fsObj."

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Thanks - Does this also work with web servers, eg:

myFile="../afile.html"

And also there isn't a cross-browser version is there?

A computer always does what you tell it to, but rarely does what you want it to.....
 

Do you mean:

1 "can the client-side script detect if files server-side",

2 "can the javascript be run server-side to detect files server-side", or

3 something else?

If 1, no - how could this ever work?
If 2, yes, assuming your server can run JS (e.g. IIS running ASP)

Dan



[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
To be honest, I'm not quite sure what I'm trying to do.

On my website I use IFrames. There is a 'left' frame and an 'upd' frame. On the page (not in an IFrame) there is a link that will link the user to a page titled name+"S".

This is the code that I have got on OnClick on the link:

function changeIt()
{
var enwffeil = upd.location.href;
enwffeil = (enwffeil.replace(ffoldercychwyn,""));

var darnau = enwffeil.split(".");
enwffeil = darnau[0] + "S" + "." + darnau[1];

*****PSEUDO*****

if enwffeil exists then

enwffeil = upd.location.href;

end if

return enwffeil;

}

// -->

But the problem is, if for any reason that the name+"S" does not exist then it shows 404 in the frame!


A computer always does what you tell it to, but rarely does what you want it to.....
 

Making a custom 404 page server-side would be one option. You could possibly try setting a handler for the frame's onerror event, although I've never tried this.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top