Closing Windows Via Scripts and Security
Ever notice how a script that attempts to close a window will at times generate a confirmation and other times not?
Here's why: a
non-JavaScript-generated window with more than one page in its history cannot be closed without asking the user for confirmation. At least this is the case with NN 4.x and IE browsers. Netscape 6.x browsers
do allow the window to be closed without confirmation.
To close a window unconditionally, a privilege called UniversalBrowserWrite is needed. Without the privilege, the window.close() method closes only windows that have been created via the JavaScript open() method without the user's permission. In other words, scripts cannot close
main browser window.
The one exception is if the window has only the current document in its session history, the close is allowed without the confirmation. The purpose of this exception was for windows that needed to open other windows then close themselves.
If a
script generates a new window, that window can be closed via scripting without a confirmation regardless of the number of documents in its history.
NOTE: for information on writing scripts to a document as in this example, visit the
Strings Lesson.
Test your current window: this script attempts to close this window via the
closeMe() function.
function closeMe(){
self.close()
}
In the case that the conditions are right for allowing a closure without confirmation, try the
reOpen() function which calls the onunload event handler to re-launch this page upon closing.
function reOpen(){
window.onunload=window.open('windowClose.php')
self.close()
}
The short of it: a script that attempts to close a window that is not a JavaScript sub window (except in the case the window has only the current document in its history) will ask the user for confirmation. However, a script that attempts to close a script-generated sub window is no sweat.
For more info on UniversalBrowserWrite, see
JavaScript Security
View the Source