[Javascript] Opening a new Window

David T. Lovering dlovering at gazos.com
Sat Apr 19 08:10:02 CDT 2003


Nothing easier.  The parent (that launches both windows, and need not be actually visible
itself), can close one at the time it opens another.

    var win1 = window.open("","Window-1","height=300,width=400,menuvar=no,scrollbars=no,statusbar=no");
    if (win1) {
      win1.document.open("text/html");
      win1.document.write("<script> document.title = 'Window-1'; </script>");
      win1.document.write("<center><h3>Window-1</h3></center>");
      win1.document.close();
    }

    // some time passes, and the user does stuff -- which can be simulated by a timer

    var timer_00 = setTimeout('if (win1) { win1.close() }', 10000);

    var win2 = window.open("","Window-2","height=300,width=400,menuvar=no,scrollbars=no,statusbar=no");
    if (win2) {
      win2.document.open("text/html");
      win2.document.write("<script> document.title = 'Window-2'; </script>");
      win2.document.write("<center><h3>Window-2</h3></center>");
      win2.document.close();
    }

You can, if you don't mind tracking through all the DOM layers, actually have the second window close
the first through the parent:

  (assuming win1 is still open, this code would exist in win2...)

    window.opener.win1.window.close();

Mind you, it's been a long time since I've tested this, and I've no idea whether it would work
equally well in NN and IE, but it is consistant with the DOM documentation.

-- Dave Lovering
      

Tim Makins wrote:
> 
> This page:
> http://mmm.eircom.ie/maps/map_Pop.asp
> 
> when opened seems to open a new window and close the old one. How do you do
> that ?
> 
> Tim in Ireland.


More information about the Javascript mailing list