Entry
How can I force a user to close my window using my "Exit" button instead of the window X button?
Oct 24th, 2000 15:00
Tim Powell, Rey Nuņez, Ahmed Ibrahim, http://www.webreference.com/dhtml/diner/beforeunload/index.html
You can provide the following code that lets a user close the window
from an "Exit" link, but there is no way to stop them from clicking on
the X, simply because there is no way to disable that.
<a href="#" onmouseover="status='Exit and close this window';return
true" onclick="self.close();return false">Exit</a>
Many people asking for a way to force the user to close the window
using a particular button are desiring a way to cleanup JavaScript code
or want to warn the user that data may be lost in a Web application.
There are two JavaScript events that can be helpful with this:
onUnload and onBeforeUnload
onUnload is supported by virtually every JavaScript aware browser. The
event is called when the page has been unloaded. This means that there
is no way to cancel this event and stop them from leaving the page, but
it may be useful for cleanup (or perhaps to set a cookie using
JavaScript to save state information).
To correct the problem that onUnload cannot be cancelled, Microsoft
introduced the onBeforeUnload event. For more information see this
WebReference article that describes the event in detail
http://www.webreference.com/dhtml/diner/beforeunload/index.html
The only limitations to the onBeforeUnload event is that it is
currently only supported in IE4+ and it forces a fairly wordy dialog to
be displayed.