As above there is no 'direct' way of testing if the page is being navigated awy from or the window is being closed.
in IE (think it was from about version 5) there is an onBeforeUnload event. it isn't an event handelr as such, but it allows you to return a message to the user with yes/no options to continue unloading
window.onBeforeUnload = new Function("return \'All changes will be lost!\'");
there are ways to test if some methods of navigation to prevent this, for example you might wish to keep a global variable (call it allowUnload) and modify its values before continuing.
eg
<script language="Javascript">
<!--
var allowUnload=0;
on window.unBeforeUnload = new Function("if(allowUnload==
//-->
</script>
<a href='wherever.asp' onlcick='allowUnload=1'>
Again however this is as close to preventing a window closing as you can come without the rather instrusive approach of opening a new window via the window.onUnload event handler.
Main Topics
Browse All Topics





by: justinbilligPosted on 2005-07-20 at 04:07:52ID: 14483124
Nope, you can use the onunload event, but you can't stop the browser from closing, also the onunload event has no way of knowing if you clicked the closed box or just reloaded the page.