You will likely want to use the "onbeforeunload" event too, it provides more control than the "onunload" event.
That said, be aware that: Opera doesn’t fire the unload event when the browser refreshes the page, or uses the back/forward buttons to browse off of the page. What’s worse, Opera never fires the onbeforeunload event. Thus if you are supporting Opera - be aware of these issues.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…