Closed Thread Icon

Topic awaiting preservation: If file exists (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=25496" title="Pages that link to Topic awaiting preservation: If file exists (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: If file exists <span class="small">(Page 1 of 1)</span>\

 
ZaddyDog
Bipolar (III) Inmate

From:
Insane since: Apr 2005

posted posted 04-13-2005 01:55

Can Javascript check if a file exists and then redirect?
On the client side that is.

Imagine an html based help application - it is meant to be local - no server .

If a certain link is clicked, I would like the file loaded to be based on a condition - i.e. that it exists.
If the file exists it is loaded.
If the file does not exist, then another file is loaded in its place.

I was unable to find a clientside script.
I did find redirection scripts, and I did find a script that tested to see if an image file was loaded.
I guess I could combine the two and look for an image file as proof does the file exist....

but is there a simpler way - anyone know a script that would do this?

In passing our Info Tech gurus said this impossible because Javascript, for security reasons, would not allow it.
i'd like to challenge that.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 04-13-2005 09:28

[uber_quick_reply]

Using the XML HTTP Request object you can make a HEAD HTTP request to retrieve the status of an URL. Check the 4th example of the page. There certainly is a constraint of cross domain.
So long, hope that helps,

[/uber_quick_reply]



(Edited by poi on 04-13-2005 09:34)

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 04-13-2005 15:16

It's certainly possible, especially if it's all local. Javascript's security would prevent you from fetching things remotely, but that doesn't seem to be what you want.

I do think XMLHttpRequest is the only way to do this. Alternatively, you could write a small CGI-style program that does things for you... but locally that's a mess (and, in fact, I don't think you could execute said program for security reasons anyway, so it probably MUST be done with XMLHttpRequest).

Apple also has a good site on XMLHttpRequest: http://developer.apple.com/internet/webcontent/xmlhttpreq.html

---
"Consider a simple room with only four walls, a ceiling and a floor. Can you see it in your mind?s eye? You better not be able to; I haven?t specified a light source yet." - Paul Nettle

ZaddyDog
Bipolar (III) Inmate

From:
Insane since: Apr 2005

posted posted 04-13-2005 23:48

Thanks for the links, i'll take a look.

BillyRayPreachersSon
Bipolar (III) Inmate

From: London
Insane since: Jul 2004

posted posted 04-19-2005 13:42

If IE-only is OK, then you can use the FileSystemObject ActiveX control to do this.

If it is an image you want to detect the presence of, you can use the onerror event of the image object (works in IE, FF, NN, opera, and Safari, I believe) to detect if it missing or not.

Hope this helps,
Dan

« BackwardsOnwards »

Show Forum Drop Down Menu