mabs mabs - 1 year ago 87
Javascript Question

Handling of child and grandchild windows in Javascript

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?

Answer Source

In the third wndow you put in:

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

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

      grandMother.document.getElementById("myDiv").firstChild.nodeValue ="Greetings from your grandchild !-";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download