When window.open () is called, the return value is a handle to the newly created window. Using this, you can save an array of windows that you opened, and then close them in the unload event handler:
var win = winodw.open(URL, title, options);
window.MyOpenWindows.push(win);
Later in the function registered for the unload event:
function closeWindows(){
for (i=0;i<window.MyOpenWindows.length;i++)
{
window.MyOpenWindows[i].close();
}
}
source
share