PDA

View Full Version : How to close a popup window on closing some other window



Asty
06-16-2008, 03:47 PM
My problem is little peculiar.
Parent1.jsp opens a popup window Popup1.jsp which opens another popup Popup2.jsp. As soon as Popup2.jsp is opened, Popup1.jsp closes itself.

Now I need to close Popup2.jsp when Parent1.jsp is closed.

using Window.opener doesn't work because then as soon as Popup1 is closed, Popup2 is also closed.

Can anyone please tell me how to do this?

Thanks.

jscheuer1
06-16-2008, 04:29 PM
When you open the second pop up, you could assign it as the value of a variable on the original parent (the page that will, presumably still be open, Parent1.jsp from your post).

So on Popup1.jsp, when you open Popup2.jsp, you could do it something like:


window.opener.pop2 = window.open('Popup2.jsp');

That way, even after Popup1.jsp is closed, on Parent1.jsp you can do:


window.onunload = function(){
if(typeof pop2 != 'undefined')
pop2.close();
}

To close it when Parent1.jsp closes. Opera doesn't usually (maybe never) fire onunload though - just a warning, almost all others do. Also, there will be a fleeting error reported in some browsers if pop2 is already closed. The usual sorts of things (if you are opening and closing pop ups you probably know about them) could be done to prevent that.

Asty
06-18-2008, 06:23 AM
Thanks a lot.

This works fine. Just a small change:

window.opener.pop2 = window.open('Popup2.jsp');

Thanks!