You are here: irt.org | FAQ | JavaScript | VFAQ | Q1578 [ previous next ]
Netscape Navigator ignores any attempt to catch the window closing. In Internet Explorer 4+ you can use the onBeforeUnload event handler.
The code below will do the trick EXCEPT in Netscape Navigator when the user hits alt-F4 or clicks the [X] in the top right corner (on windows and possibly other platforms):
<html>
<head>
<script language="JavaScript"><!--
function confirmIELeave() {
event.returnValue = "Before you go, please turn off the light...";
}
function confirmNSLeave(iWindow) {
if (!document.layers) return;
if (!iWindow.closed){
if (!confirm('Are you sure you want to leave this page?'))
window.location.href = 'javascript:void(0)';
}
else window.open(location.href,'newwin');
}
//--></script>
</head>
<body onbeforeunload="confirmIELeave()" onUnload="confirmNSLeave(this)">
<p>Navigate to another page to fire the before unload event. <a href="http://www.netscape.com">go</a>
</body>
</html>Here is a test to show you how Netscape Navigator fails to detect alt-F4:
<html>
<head>
<title>Testing universalbrowserwrite's authority</title>
<script language="JavaScript1.2"><!--
function doit() {
if (document.layers) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserWrite');
window.open('noalt.htm','newwin','titlebar=no,width=800,height=600')
}
}
//--></script>
</head>
<body>
<a href="javascript:doit()">Open without titlebar</a>
</body>And then in oalt.html:
<script language="JavaScript"><!--
if (document.layers) {
window.captureEvents(Event.KEYPRESS);
window.onkeypress=cnclalt;
}
function cnclalt(e) {
alert(e.modifiers);
if (e.modifiers==1) {
alert('Please log out');
return false;
}
}
//--></script>Try to press alt here! Nothing! Try alt spacebar - it gives you the system menu. Change the script to e.modifiers==2 and press shift and you get the alert!