Entry
Can I capture closing the window?
Can I ask for confirmation closing the window?
Mar 23rd, 2000 11:54
Martin Honnen, Yakov Simkin, microsoft.public.scripting.jscript
IE and NN have the
<BODY ONUNLOAD="// some js here">
event handler which fires when the current document becomes unloaded
that is when the window is closed, when a link is followed or otherwise
a new url is loaded.
There are two main problems with onunload, in particular with NN:
1) alert/confirm/prompt in onunload get swallowed if the the reason for
unload is closing the window
2) the document is already unloaded as a js object too so scripting
document objects like forms in onunload doesn't work
So onunload is of limited reliable use.
IE4/5 has a remedy for that by providing
<BODY ONBEFOREUNLOAD="// some js here">
which doesn't have the problems of onunload and even allows to ask for
confirmation of the unloading by setting
event.returnValue = 'Are you sure you want to leave the page?';
e.g.
<BODY ONBEFOREUNLOAD="event.returnValue =
'Are you sure you want to leave the page?';">
Note that onbeforeunload still fires for every kind of unload so there
is no way to capture just window closing with it.