2

How can I check if a .htm file exists on local disk using Javascript when the .htm file that contains the Javascript code is loaded locally (not from a web server)?

1
  • Cross browser, or just IE? Normal web page security or trusted site? Commented Mar 2, 2011 at 6:48

2 Answers 2

5

This solution works on most all versions of IE and FF. Doesn't work on Chrome when running from local disk.

I'm using XHR and old IE ActiveX control in synchronous mode. You can easily convert it to run async with the onreadystatechange callback.

In your own Javascript code, just call IsDocumentAvailable("otherfile.htm") and you should be set.

    function IsDocumentAvailable(url) {

        var fSuccess = false;
        var client = null;

        // XHR is supported by most browsers.
        // IE 9 supports it (maybe IE8 and earlier) off webserver
        // IE running pages off of disk disallows XHR unless security zones are set appropriately. Throws a security exception.
        // Workaround is to use old ActiveX control on IE (especially for older versions of IE that don't support XHR)

        // FireFox 4 supports XHR (and likely v3 as well) on web and from local disk

        // Works on Chrome, but Chrome doesn't seem to allow XHR from local disk. (Throws a security exception) No workaround known.

        try {
            client = new XMLHttpRequest();
            client.open("GET", url, false);
            client.send();
        }
        catch (err) {
            client = null;
        }

        // Try the ActiveX control if available
        if (client === null) {
            try {
                client = new ActiveXObject("Microsoft.XMLHTTP");
                client.open("GET", url, false);
                client.send();
            }
            catch (err) {
                // Giving up, nothing we can do
                client = null;
            }
        }

        fSuccess = Boolean(client && client.responseText);

        return fSuccess;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I had only to modify the fSuccess assignment to: fSuccess = Boolean(client && client.responseText && client.status != 404);
Cool. What is client.responseText when status is 404 off the filesystem? Depending on different browsers and depending whether the content is local or on a webserver, you may get back "0" or "200" for success. Test the true and false case with different browsers.
I don't know why this solution has to be so long, but it does work great! Thanks!
0

Assuming the htm file is on the same domain, you can do this:

function UrlExists(url) {
  var http = new XMLHttpRequest();
  http.open('HEAD', url, false);
  http.send();
  return http.status!=404;
}

This will not work on the local file system on several browsers, such as Chrome, because of domain security restrictions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.