views:

417

answers:

5

My main HTML page does the following:

var popup = window.open(...);
// Wait for popup to load
popup.onload = function() { do_something(); };
popup.location = "new-page.html";

I'd like do_something to be called when new-page.html is finished loading. But what I currently have doesn't work -- do_something is never called.

How can I make this work?

I only care about getting this to work in FF 3.5, if that makes things easier.

+3  A: 

you could use window.opener.<functionName> and put your notification code within that function on the parent page

Anthony Shaw
That's exactly what I needed. Thanks!
Justin L.
Beware of window.opener. It's proprietary IE, and not part of any standards recommendation, and people have had problems with this in firefox:codingforums.com/showthread.php?t=44015
James Wiseman
+1  A: 

Have you tried using the opener object?

http://www.webreference.com/js/tutorial1/opener.html

A: 

Try having the doSomething method called in the popup's onload. You can refer back to the parent by using window.opener

Instantsoup
A: 

In your .html that is loaded you could have a simple one liner at the end that calls a function on the opening window (if it exists)

<script type='text/javascript'>
if (window.opener && window.opener.do_something) window.opener.do_something(window);
</script>
gnarf
A: 

Or, without messing with the HTML of the child window, from the parent, you can attach a listener on the load event of the iframe. In this solution, i am going to use jQuery for simplicty, but any library or even manual event attachment will do.

In the parent's HTML:

<iframe id="foo"></iframe>
<script type='text/javascript'>
    var element = $("#foo");
    element.load( function( eventObject ) { 
        // do something here
    } );
    // Now that the event listener is attached, we can set the source.
    element[0].src = "foo.html";

</script>

Note, I have not tried setting the source first and then attaching the listener, I am not sure if this would be guaranteed to always work. It could happen that the iframe is created/loaded before the load listener is attached, I am not sure. But, by setting the src after the listener is attached, I am sure that the load listener will be called.

Shane

http://www.shanetomlinson.com

http://www.ubernote.com

Shane Tomlinson
I'm curious if you can get this to work without jQuery and with a popup instead of an iframe. This is basically what I was trying to do in the example I originally gave, but I couldn't make it work. (IIRC, I was able to make it work by setting `iframe.onload = do_something`.)
Justin L.