Event to be triggered when leaving website or closes the browser
问题 I´m trying for a while execute a javascript function when a user leaves web site by typing a address in the browser and hits return or the user closes the brower clicking in the 'x' button. I tried the unload event but I cannot get the alert this is what I am doing: $(window).unload(function () { alert("Are you sure?"); }); also $(body).unload(function () { alert("Are you sure?"); }); I am running out of idea! 回答1: You can listen to beforeunload event that fires before the unload event, when