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? CORS is not an option. I only care of its existence.
是否存在仅客户端方法来检测托管在另一个域上的xml文件是否丢失(响应404)或使用Internet Explorer可用? CORS不是一种选择。我只关心它的存在。
A client-side only method exists for Chrome and Firefox by combining <script>
tag injection with callbacks to the 'load' and 'error' events. Below is the test code I place in the browser console for Firefox and Chrome.
通过将
In Internet Explorer, the 'readystatechange' event always fires regardless if the file exists or not. 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.
在Internet Explorer中,无论文件是否存在,“readystatechange”事件始终会触发。我已经从'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.
我还尝试了 tag和
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);
1 个解决方案
#1
0
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,如果它确实存在且加载到脚本中,浏览器在分析文件时会抛出语法错误,如果它返回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
希望这可以帮助
#1
0
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,如果它确实存在且加载到脚本中,浏览器在分析文件时会抛出语法错误,如果它返回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
希望这可以帮助