Detect xml download failure in Internet Explorer using script tags

Is there only a client-side method to determine if an xml file is hosted in another domain (404 response) or is accessible via Internet Explorer? CORS is not an option. I only care about its existence.

For Chrome and Firefox, there is only a client-side method, combining <script>tag insertion with callbacks for the download and error events. Below is the test code that I put in the browser console for Firefox and Chrome.

In Internet Explorer, the "readystatechange" event always fires regardless of whether the file exists or not. I looked at the returned object from the readystatechange callback, and I cannot find the difference between the response object from the existing file and the response object from the nonexistent file.

I also experimented with tag <img>and tag tag <iframe>, and the results are not as useful as pasting a tag <script>for any browser.

function loadFile(urlOfDocument) {
    var element = document.createElement('script');
    element.async = true;

    element.onload = function() {
        // Works for Chrome and Firefox
        console.log("onload called");
    }
    element.onerror = function() {
        // Works for Chrome and Firefox
        console.log("onerror called");
    }
    element.onreadystatechange= function () {
        // IE 8, 9, 10
        console.log("onreadystatechange: ", element.readyState);
        if (this.readyState == 'complete') {
            console.log("loading complete");
        }
    }
    element.src = urlOfDocument;
    document.getElementsByTagName('head')[0].appendChild(element);
}

var urlOfDocument = "http://url.to.missing.file";
loadFile(urlOfDocument);
+4
source share
1 answer

, . XML, script, syntax error, , vs, 404, , IE

    if (this.readyState == 'loading') {
       window.onerror = function(e){
          if(! (e === "Syntax error") ) return;
          console.log("File Exits");
          window.fileExists = true;
       }
    }
   if (this.readyState == 'loaded') {
       //if we got here without an error the file is missing. 
       if(!window.fileExists){
           console.log("File is missing");
       }
       //clean up
       window.onerror = "";
    }

: script.

,

0

All Articles