简体   繁体   中英

How to close all children windows in browser on unload of parent window with javascript

I need to find and close all children windows (whose that this page is their window.opener) on unload of the page with javascript. When parent window is about to be unloaded I need to run through all opened windows and close only those that the unloaded page is their window.opener. What is the correct way to do so ? I need a cross-browser solution .

Create a variable for every new window you need to open, then close them like this:

var.close();

Full example:

var window1; 
window1 = window.open("www.google.com");
window1.close(); 

Try making use of onunload event which is responsible for closing all the pop-ups.

<body onunload="destroyPopUps()">

In the code above the onunload event is tied to the body element of the page. Here is the implementation of the destroyPopUps function.

function destroyPopUps() 
{
    if(popups.length == 0) return; 
    for(i=0; i<popups.length; i++) 
    {
        popups[i].close(); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM