等待子窗口加载完成 [英] Waiting for child window loading to complete
问题描述
是否有一个简单的钩子来检测脚本打开的窗口是否已完成加载?基本上,我想要相当于 onLoad()
的钩子,但我不能直接设置它 - 假设子文档是给定的,我实际上不能把我自己的代码放进去。
Is there an easy hook for detecting that a window opened by a script has finished loading? Basically, I want the equivalent of the onLoad()
hook, but I can't set it directly -- assume that the child document is a given and I can't actually put any code of my own in it.
例如,假设我有以下两个文件:
For instance, say I have the following two files:
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()
调用爆炸。如何检测孩子已完成加载?
Since setting location.href is non-blocking, w.doSomething()
isn't defined yet and the doSomething()
call blows up. How can I detect that the child has finished loading?
推荐答案
如果新打开的窗口的位置是同源的,则此方法有效:
This works if the location of the newly opened window is same-origin:
var w = window.open('child.html')
w.addEventListener('load', w.doSomething, true);
这篇关于等待子窗口加载完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!