Developer

Prevent window orphans

There are times when breaking the object hierarchy is harmless, if not desirable. Here's how to create a transitional pop-up window that automatically closes when it launches a new window.

By Brent Lee Metcalfe

This tip is a warning: If you open a secondary window and a tertiary window that is a child to the secondary window, then close the secondary window; the secondary window's parent can no longer control the tertiary window. Closing an intermediate window severs the (object) hierarchical relationship between both the parent and child windows of the intermediate window.

That said, there might be occasions where breaking the object hierarchy is harmless, if not desirable. Consider a transitional pop-up window that automatically closes when it launches a new window.

The secondary pop-up window includes a function that tests whether the tertiary window is already open:

var whoAmI = navigator.appName;

function transitory(url) {
  var hobo = window.open(url,'bum','scrollbars,resizable,width=640,height=340,left=40,top=40');
  if (hobo != null) window.close();
  if (whoAmI == 'Netscape' && hobo != null) hobo.focus();
}

When the tertiary window opens, the secondary pop-up window closes. If the tertiary window is already open, function transitory() does some browser-specific focusing."

If you set the variable for the tertiary window's open() method to null, you'll generate an error in IE 4.x. Carefully weigh the tradeoffs of breaking the window object hierarchy.

Brent Lee Metcalfe is a Web architecture consultant.

Editor's Picks