Javascript 如何处理加载 iframe 时出现的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3705083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I handle errors in loading an iframe?
提问by James A. Rosen
I have an <iframe>that other sites can include so their users can POSTa form back to my site. I'd like to handle gracefully the cases where my site is down or my server can't serve the <iframe>contents (that is, a response timeout or a 4xx or 5xx error). I tried adding an onErrorto the <iframe>object, but that didn't seem to do anything:
我有一个<iframe>其他网站可以包含的内容,因此他们的用户可以POST将表单返回到我的网站。我想优雅地处理我的站点关闭或我的服务器无法提供<iframe>内容(即响应超时或 4xx 或 5xx 错误)的情况。我尝试添加一个onError到<iframe>对象,但似乎并没有做任何事情:
showIFrame = function() {
var iframe = document.createElement('iframe');
iframe.id = 'myIFrame';
iframe.src = 'http://myserver.com/someURLThatFailsToLoad';
iframe.onError = iframe.onerror = myHandler;
document.body.appendChild(iframe);
};
myHandler = function(error) {
document.getElementById('myIFrame').style.display = 'none';
console.error('Error loading iframe contents: ' + error);
return true;
};
If my server returns a 404I just get the contents of the not-found page in my <iframe>. In fact, that error handler isn't ever triggered. Is there a way to make this work?
如果我的服务器返回 a404我只是在我的<iframe>. 事实上,该错误处理程序从未被触发。有没有办法使这项工作?
(I'm currently testing in Chrome, but I'd like it to also work for FF and IE >= 7.)
(我目前正在 Chrome 中进行测试,但我希望它也适用于 FF 和 IE >= 7。)
采纳答案by Marcel Korpel
To detect whether your server is down or not, you can include an empty script file from your own domain. When the server is down, the onerrorevent handler will fire:
要检测您的服务器是否已关闭,您可以包含一个来自您自己域的空脚本文件。当服务器关闭时,onerror事件处理程序将触发:
var el = document.createElement('script');
el.onerror = errorFunction;
el.src = "somebogusscript.js?" + new Date().getTime();
document.body.appendChild(el);
Note: don't forget to add a random string to the srcattribute to avoid the client using a cached version (which could stop a look at the server at all).
注意:不要忘记向src属性添加一个随机字符串以避免客户端使用缓存版本(这可能会停止查看服务器)。
回答by Joel Kennedy
Perhaps you could try onErrorUpdatefor the event handler? I couldn't see an onErrorhandler for iFrames. If that doesn't work, you could try onLoadand then check the source of the iframe or the title of it for a 404 message.
也许您可以尝试onErrorUpdate使用事件处理程序?我看不到onErroriFrame的处理程序。如果这不起作用,您可以尝试onLoad然后检查 iframe 的来源或 404 消息的标题。
Such as:
if (frameDoc.title == 'title the server sends for 404') {
如:
if (frameDoc.title == 'title the server sends for 404') {
Source:
来源:
http://bytes.com/topic/javascript/answers/166288-catch-404-when-using-iframe
http://bytes.com/topic/javascript/answers/166288-catch-404-when-using-iframe
iFrame Methods: http://www.java2s.com/Code/HTMLCSSReference/HTML-Tag-Reference/iframeJavaScriptMethods.htm
iFrame 方法:http: //www.java2s.com/Code/HTMLCSSReference/HTML-Tag-Reference/iframeJavaScriptMethods.htm
iFrame Properties: http://www.java2s.com/Code/HTMLCSSReference/HTML-Tag-Reference/iframeJavaScriptProperties.htm
iFrame 属性:http: //www.java2s.com/Code/HTMLCSSReference/HTML-Tag-Reference/iframeJavaScriptProperties.htm
回答by Sam Watkins
One technique is to set a JavaScript timeout when you make the request. If your timeout fires before the iframe onload event, the content didn't load. You could then set iframe.src to about:blank, delete, or reuse the iframe.
一种技术是在您发出请求时设置 JavaScript 超时。如果您的超时在 iframe onload 事件之前触发,则内容未加载。然后,您可以将 iframe.src 设置为 about:blank、删除或重用 iframe。

