Question

I have a main window (#1) on my webpage from which I open a new browser window (#2) from which I open a new window (#3).

Now if my user closes window#2 before window#3, I have the problem that window#3 no longer can call function in its window.opener since it has gone away.

What I would like to do is to set window#3.opener to window#1 when window#2 closes.

I've tried to do this i window#2 (by the way I use jquery):

var children = [];
$(window).unload( function( ) {
    $.each( children, function( p, win ) {
        if ( win ) {
            win.opener = window.opener;
            }
    } );
} );

When window#3 is loaded I add the window to the array children in window#2.

But still when window#2 is closed before window#3, windows#3's window.opener doesn't point to window#1.

How do I make sure that my grand child window (window#3), can still call the main window (window#1) after window#2 is closed?

Was it helpful?

Solution

In the third wndow you put in:

<script type="text/javascript">
  var grandMother = null;
  window.onload = function(){
    grandMother = window.opener.opener;
  }
</script>

Thus you have the handle to the grandmother-window, and you can then use it for anything directly:

if(grandMother)
      grandMother.document.getElementById("myDiv").firstChild.nodeValue ="Greetings from your grandchild !-";

OTHER TIPS

You should create a reference to the main window when the third opens:

parent = window.opener.opener

This will survive the second window closing.

window.opener probably is read-only. I'd setup your own property to refer to the grandparent when grandchild is loaded.

function onLoad() {
  window.grandparent = window.opener.opener;
}

This may be tangential, but why do you need to open 3 separate windows? Can you use a jQuery dialog instead? I get very frustrated when apps open windows on me.

var main_window = get_main_window();
function get_main_window(){
    var w = window;
    while(w.opener !== null){
        w = w.opener;
    }
    return w;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top