最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I notify the parent page when a popup has finished a load which the parent initated? - Stack Overflow

programmeradmin2浏览0评论

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 Firefox 3.5, if that makes things easier.

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 Firefox 3.5, if that makes things easier.

Share Improve this question edited Sep 6, 2022 at 14:14 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 12, 2009 at 19:29 Justin L.Justin L. 4,1473 gold badges34 silver badges31 bronze badges 1
  • 1 :) I'm not using this on a production website; I in fact need this to work so I can test a patch I'm writing for Firefox. – Justin L. Commented Aug 12, 2009 at 20:45
Add a ment  | 

5 Answers 5

Reset to default 4

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

Have you tried using the opener object?

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

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.

http://www.ubernote.

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

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>
发布评论

评论列表(0)

  1. 暂无评论