0

I tried using the two different methods included below. The first which exclusively uses JavaScript and the second method which uses jQuery. Both gave the same result which was that it worked fine in Firefox but failed at "http.send()" in both Chrome and Internet Explorer 11. Lastly I tried appending 'chrome.exe" --allow-file-access-from-files', but nothing works. Are Chrome and IE too secure to function? Any suggestions are welcome.

// This function checks to see if the file really exists and returns TRUE if it does.
function urlExists(url) {
    var http = new XMLHttpRequest();
    try {
        http.open('HEAD', url, false);
        http.send();
    } catch (err) {
        silenterror = err;
        return false;
    }
    return http.status === 200;
}

// This function checks to see if the file really exists and returns TRUE if it does.
function urlExists(testUrl) {
    var http = jQuery.ajax({
        type : "HEAD", //Not get
        url : testUrl,
        async: false
    });
    return http.status === 200;
}
Doug
  • 73
  • 1
  • 8
  • 1
    Can you give an example of what 'testUrl' you are passing into urlExists? – dannypaz Aug 15 '15 at 03:33
  • 1
    possible duplicate of [File Url Cross Domain Issue in Chrome- Unexpected](http://stackoverflow.com/questions/6060786/file-url-cross-domain-issue-in-chrome-unexpected) – ThatOneDude Aug 15 '15 at 03:40
  • @ssnobody what makes you think there's any cross domain involved – Jaromanda X Aug 15 '15 at 03:42
  • Because the OP stated he's using file:// url's implicitly from the chrome option he is trying, and you will violate cross-domain policy trying to access ajax urls from there. – ThatOneDude Aug 15 '15 at 03:43
  • @Doug what doesn't the chrome console in developer tools show? – ThatOneDude Aug 15 '15 at 03:44
  • As a note, I believe the developer is using a local file url to load the page, but attempting to load non file urls via ajax, but @JaromandaX does validly point out that I'm making an assumption. – ThatOneDude Aug 15 '15 at 03:48
  • @ssnobody - I see no mention of file:/// urls in the question - you are right, chrome thinks accessing file:/// from a file:/// is cross domain though - but that's misinformation, chrome just doesn't allow xmlhttprequest on file:/// urls ... to summarise ... firefox does allow file:/// xmlhttprequests from file:/// resources, chrome incorrectly reports it as a cross origin violation, and IE Edge can't even load file:/// urls any more! (maybe an option I haven't switched on) – Jaromanda X Aug 15 '15 at 03:49
  • `--allow-file-access-from-files` : _By default, file:// URIs cannot read other file:// URIs. This is an override for developers who need the old behavior for testing._ Since OP included this option in trying to solve his problem it's a reasonable assumption to believe they are indeed trying to read from file urls. – ThatOneDude Aug 15 '15 at 03:53
  • These are code snippets from a web linker I put together that opens either a PDF or HTML file depending on if the PDF file exists. If it does, it displays it, if not a default page is displayed showing where to download the requested file. Everything is being ran on the local machine. – Doug Aug 15 '15 at 04:00
  • "XMLHttpRequest cannot load file:///C:/Users/user1/Documents/doug_links/local_files/schedule/schedule.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource." – Doug Aug 15 '15 at 04:10
  • Please add error to question and try to --disable-web-security as stated in duplicate suggestion http://stackoverflow.com/questions/6060786/file-url-cross-domain-issue-in-chrome-unexpected – ThatOneDude Aug 15 '15 at 04:16

0 Answers0