You can use:
$(window).unload(function() {
//do something
}
Unload() is deprecated in jQuery version 1.8, so if you use jQuery > 1.8 you can use even beforeunload instead.
The beforeunload event fires whenever the user leaves your page for any reason.
$(window).on("beforeunload", function() {
return confirm("Do you really want to close?");
})
Source Browser window close event
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…