When we call window.close(), it closes the popup windows, that are opened up using the window.open(…) method, without any issue. But when we try to call the method window.open on a main window, that was not opened up using the window.open(…) method, window is not closed by default and a confirm box is shown first.
In the method window.close(…) javascript simply checks if the window.opener is Null or not, if it is Null a confirm box is shown. So the trick is to set some value in the window.opener variable, this way javascript will treat this window as a popup window and the window.close will work without any message. You can try the following code yourself to experience this:
/*This function closes the passed (or the current) window without showing the confirm box even if the window is not opened using the window.open method */
function closeMainWindow(win)
{
if (!win) //If no windows is passed - close the current window
{
win = window;
}
win.opener=‘X’;
//This will not show the confirm dialog before closing the window.
//Javascript will treat this window as a Popup.
win.open(”, ‘_parent’, ”);
//Only for IE-7, this line is not needed for IE6
win.close();
//Now the call to window.close() will not display any warning/confirm dialog on IE6 and IE7
}