Is there any JavaScript / jQuery code between browsers to detect whether the browser or a browser tab is being closed?
Is there any JavaScript / jQuery code between browsers to detect whether the browser or a browser tab is being closed?
Use the .unload()
event.
link
Jquery < 3.0
$(window).unload(function() {
alert("call");
console.log("this will be triggered");
});
Jquery > = 1.7
$(window).on("unload", function(e) {
alert("call");
console.log("this will be triggered");
});
The unload event is sent to the window element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing the browser window will cause the event to be triggered. Even a page reload will first create an unload event.
The unload event is sent to the window element when the user navigates off the page. This can mean one of many things. The user could have clicked on a link to leave the page or enter a new URL in the address bar. The forward and backward buttons will trigger the event. Closing the browser window will cause the event to fire. Even a page reload first will create an unload event.
google translator