javascript 我可以在 Iframe 的父窗口中捕获 Iframe 的异常吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6327128/
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
can I catch exception of Iframe in parent window of Iframe
提问by Arif Hasan
I have an IFrame in a page and IFrame has some JavaScript. At run time JavaScript in IFrame gives exception which i want to catch on parent window. How to do that?
我在页面中有一个 IFrame,而 IFrame 有一些 JavaScript。在运行时 IFrame 中的 JavaScript 给出了我想在父窗口上捕获的异常。怎么做?
<html>
<head>
<script type="text/javascript">
var frm123 = document.getElementById("frm123");
frm123.contentWindow.onerror = function() {
alert('error caught');
}
function loadData() {
var oRTE = document.getElementById("frm123").contentWindow.document;
oRTE.open();
oRTE.write(txt123.value);
oRTE.close();
}
</script>
</head>
<body>
<input type="button" onclick="loadData();" value="load">
<input type="text" id="txt123">
<iframe id = "frm123" >
</body>
</html>
回答by TheZuck
The answer depends on whether or not you have control of the iframe code, and whether or not it is the same domain.
答案取决于您是否可以控制 iframe 代码,以及它是否是同一个域。
If same domain then you can do the following to set the error handling function from the wrapping document:
如果域相同,则可以执行以下操作以从包装文档设置错误处理功能:
document.getElementById("myiframe").contentWindow.onerror=function() {
alert('error!!');
return false;
}
make sure you wait for the iframe to finish loading before setting the error handler.
确保在设置错误处理程序之前等待 iframe 完成加载。
If it's not the same domain but you have control of the iframe content (both domains are under your control), you can communicate with the outer frame by using a cross domain communication framework (google it or build it yourself), i.e. catch the error in the iframe by setting the onerror handler from within the iframe and send it through the framework to the outer document.
如果不是同一个域但是你可以控制iframe内容(两个域都在你的控制之下),你可以使用跨域通信框架(google it或自己构建)与外框架通信,即捕获错误在 iframe 中通过从 iframe 内设置 onerror 处理程序并将其通过框架发送到外部文档。
If it's not the same domain and you don't have control of the iframe, there's no way for the outer document to know what's going on inside it because of security constraints.
如果它不是同一个域并且您无法控制 iframe,则由于安全限制,外部文档无法知道其中发生了什么。
回答by Pratik
There may be few possible causes for .onError()
function not to work it may be because of cross domain URL's for this you should implement a check before actulally laoding the iFrame
and for that use
.onError()
功能不工作的可能原因可能很少,这可能是因为跨域 URL 的原因,您应该在实际加载iFrame
和用于该用途之前实施检查
var url = "google.com"
var loading_url = "/empty.html"
document.getElementById("iframe").src = loading_url;
$.ajax({
url: url,
type: 'GET',
complete: function(e, xhr, settings){
if(e.status === 200){
document.getElementById("iframe").src = url;
}
}
});
This will not work cross domain, the status code is 0 in those cases.
这不会跨域工作,在这些情况下状态代码为 0。
One more thing to check is .onError()
handler please check from below sample if it matches your conditions
要检查的另一件事是.onError()
处理程序,请从下面的示例中检查它是否符合您的条件
How can I handle errors in loading an iframe?
There are many other options or tweaks to handle this kind of errors :
还有许多其他选项或调整来处理此类错误:
if (frameDoc.title == 'title of page after expection occur')
if (frameDoc.title == 'title of page after expection occur')
will also do the work using on .onLoad()
event of iFrame.
还将使用.onLoad()
iFrame 事件进行工作。
回答by Mark Kahn
The onlyway is if the frame is on the same domain, in which case you can do, from parent:
的唯一方法是,如果框架是在同一个域,在这种情况下,你可以做的,从父:
iframe.contentWindow.onerror = function(){
// handle error
}