Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  47] [ 1]  / answers: 1 / hits: 71630  / 15 Years ago, thu, september 3, 2009, 12:00:00

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>


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?


More From » javascript

 Answers
17

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);

[#98766] Monday, August 31, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deiong

Total Points: 15
Total Questions: 103
Total Answers: 99

Location: Sudan
Member since Thu, May 7, 2020
4 Years ago
;