简体   繁体   English

使用脚本标记检测Internet Explorer上的xml加载失败

[英]Detect xml load failure on Internet Explorer using script tags

Is there a client-side only method to detect if an xml file hosted on another domain is missing (response 404) or available using Internet Explorer? 是否存在仅客户端方法来检测托管在另一个域上的xml文件是否丢失(响应404)或使用Internet Explorer可用? CORS is not an option. CORS不是一种选择。 I only care of its existence. 我只关心它的存在。

A client-side only method exists for Chrome and Firefox by combining <script> tag injection with callbacks to the 'load' and 'error' events. 通过将<script>标记注入与“load”和“error”事件的回调相结合,Chrome和Firefox只存在一种客户端方法。 Below is the test code I place in the browser console for Firefox and Chrome. 下面是我在Firefox和Chrome浏览器控制台中放置的测试代码。

In Internet Explorer, the 'readystatechange' event always fires regardless if the file exists or not. 在Internet Explorer中,无论文件是否存在,“readystatechange”事件始终会触发。 I've examined the returned object from the 'readystatechange' callback, and I can't seem to find a difference between a response object from an existing file and a response object from a non-existent file. 我已经从'readystatechange'回调中检查了返回的对象,我似乎无法找到现有文件的响应对象与不存在的文件中的响应对象之间的区别。

I've also experimented with <img> tag and <iframe> tag injection and the results are not as helpful as <script> tag injection for any browser. 我还尝试了<img> tag和<iframe>标签注入,结果不如任何浏览器的<script>标签注入有用。

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);

I haven't tested this fully, but it might work. 我没有完全测试过,但它可能会起作用。 Since you are looking to load a XML, if it does exist and it loads into the script, the browser will throw a syntax error when it parses the file, vs if it returned a 404, there will not be an error, so you can add the following for IE 由于您希望加载XML,如果它确实存在且加载到脚本中,浏览器在解析文件时将抛出syntax error ,如果它返回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 = "";
    }

Note: this will not work with a valid script file. 注意:这不适用于有效的脚本文件。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM