有没有一种简单的方法来检测由脚本打开的窗口是否已经加载完成?基本上,我想要与
例如,假设我有以下两个文件:
onLoad()
钩子相当的东西,但我无法直接设置它——假设子文档是给定的,并且实际上我无法在其中放置任何代码。例如,假设我有以下两个文件:
parent.html
:<html>
<head>
<title>Parent</title>
</head>
<script type="text/javascript">
var w;
function loadChild() {
w = window.open();
w.location.href="child.html";
// block until child has finished loading... how?
w.doSomething();
}
</script>
</html>
<body>
I am a parent window. <a href="javascript:loadChild()">Click me</a>.
</body>
child.html
:
<html>
<head>
<title>Child</title>
</head>
<script type="text/javascript">
function doSomething() {
alert("Hi there");
}
</script>
</html>
<body>
I am a child window
</body>
由于设置location.href是非阻塞的,因此w.doSomething()
尚未定义,而doSomething()
调用失败。我该如何检测子页面何时加载完成?
file:///
URL 一起使用,但也许这是出于安全考虑? - David Moleshttp://
混淆了? - Øystein Riiser GundersenaddEventLister
在IE中的支持不太好,因此像gabtub建议的那样使用jQuery(或另一个修复事件处理程序浏览器不兼容性的JS框架)可能是个好主意... - Øystein Riiser Gundersenfile:///
URL的安全策略非常严格(在某些情况下,甚至是有缺陷的)。尝试使用适当的Web服务器并查看是否解决了问题。此外,请尝试检查IE / Chrome控制台是否有任何错误消息。 - Øystein Riiser Gundersen