There is both window.onbeforeunload
and window.onunload
, which are used differently depending on the browser. You can assign them either by setting the window properties to functions, or using the .addEventListener
:
window.onbeforeunload = function(){
// Do something
}
// OR
window.addEventListener("beforeunload", function(e){
// Do something
}, false);
Usually, onbeforeunload
is used if you need to stop the user from leaving the page (ex. the user is working on some unsaved data, so he/she should save before leaving). onunload
isn't supported by Opera, as far as I know, but you could always set both.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…