我有多帧应用程序,其中每个表单在单独的框架中打开,并且可以从父窗口使用一些JS API。 API可以抛出异常(错误),但我无法在iframe中捕获它。它仅在IE8中复制。我已在IE8 / IE9和Chrome中检查过它。例如: -
的test.html
<html>
<head>
<script language="JavaScript" type="text/javascript">
var g_fn = {};
function onLoad() {
g_fn.thrown_error = function() {
throw new Error("Main window: error");
return 1;
}
window.g_fn = g_fn;
}
function window_do() {
try {
g_fn.thrown_error();
} catch (err) {
alert("Error catched!");
}
}
</script>
</head>
<body onload="onLoad();">
<input type="button" value="do smth in window" onclick="window_do();"/>
<iframe src="/iframe.html" />
</body>
</html>
Iframe.html的
<html>
<head>
<script language="JavaScript" type="text/javascript">
var g_fn;
function onLoad() {
g_fn = window.parent.g_fn;
}
function iframe_do() {
try {
g_fn.thrown_error();
} catch (err) {
alert("Error catched!");
}
}
</script>
</head>
<body onload="onLoad();">
<input type="button" value="do smth in iframe" onclick="iframe_do();"/>
</body>
</html>
这是IE漏洞吗?任何解决方法?
答案 0 :(得分:0)
试试这个:
g_fn = window.frameElement.g_fn;
或者,如果iframe
s中有iframe
个:
g_fn = top.window.g_fn;