tags:

views:

545

answers:

2

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?

A: 

how about

parent.html:

<html>
<head>
<title>Parent</title>
</head>
<script type="text/javascript">
  var w;
  function loadChild() {
    w = window.open();
    w.location.href="child.html";
    // like this (with jquery)
    $(w).ready(function()
    {
      w.doSomething();
    });
  } 
</script>
</html>
<body>
  I am a parent window. <a href="javascript:loadChild()">Click me</a>.
</body>
gabtub
this requires jQuery, though...may not be preferable...
peirix
Doesn't seem to do anything. :( I haven't seen the `$` operator before (I spend most of my time in Java); what's it supposed to do? The Chrome JavaScript debugger gives me `uncaught exception ReferenceError: $ is not defined`.
David Moles
Oops, cross-comment. Yeah, jQuery, that would explain why I haven't seen it. It does seem like there ought to be some way to do it just with the DOM out of the box.
David Moles
without using a JS framework, it's probably best to use gunderwonder's solution. As he said "addEventListener" is not supported in IE, but you can do a browsercheck and, in case of IE, use microsofts implementation of adding event listener => "w.attachEvent('onload',doSomething);"
gabtub
+2  A: 

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);
Øystein Riiser Gundersen
Can you clarify "same-origin"? It doesn't seem to work with `file:///` URLs on my local box, but maybe that's a security thing?
David Moles
Yes, probably. In the browser, same-origin basically means that the target must be on the same _host_ and same _protocol_. Are you mixing it with `http://`, perhaps?
Øystein Riiser Gundersen
I forgot to add that `addEventLister` is not properly supported in IE, so using jQuery like gabtub suggests (or another JS framework which fixes browser incompatibilities for event handling) is probably a good idea...
Øystein Riiser Gundersen
Not mixing, I guess Chrome just doesn't consider two `file:///` URLs the same origin? I can't get IE7 to run scripts on local files at all... Probably I should be trying this with a running server.
David Moles
That could be the case, yes. I believe the security policy for `file:///` URLs in browsers is _very_ restrictive (and in some cases, simply broken). Try it with a proper web server and see if this fixes the problem. Also, try checking the console in IE/Chrome for any error messages.
Øystein Riiser Gundersen