views:

1388

answers:

5

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?

A: 

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.

Chris Marasti-Georg
A: 

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;
}
sblundy
+1  A: 

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 !-";
roenving
+1  A: 

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

parent = window.opener.opener

This will survive the second window closing.

Diodeus
+1  A: 

@sblundy and roenving

Of cause, simple and elegant solution. Thanks.

@chris

Normally I wouldn't open new windows at all, but something like jquery's dialogs. This is a special case where it isn't practical, essentially I have to dialogs there the first can interact with the second. As I said special case, which I normally would do.

mabs