The onUload event handler has been around since JavaScript 1.0. The unload event fires when the user exits a document. It falls down though, in that it cannot cancel itself.
In web page design, this is great. The casual user browsing the web should be in complete control. In the world of DHTML, Ajax and web applications though, it becomes much more desirable. (Purists may still argue that providing a reliable ‘undo’ function is better than a cancel – but this throws up a whole heap of other questions.)

Gmail capturing a window unload event
IE has supported an non-standards compliant handler, onBeforeUnload for a while. What I just realised today, is that Firefox now supports this too. It should have been obvious – on inspection, Gmail uses just this mechanism. (Again, purists may ask why not simply send off an AJAX save request on the unload and let the user know their message is saved in drafts, where they can delete it if they want. I’ll let them!).
Certainly not something to use everyday – but I can think of a couple of apps I will retrofit to give the user a nicer experience.
Latest posts