window.beforeunload event code example
Example 1: js before unload
window.addEventListener('beforeunload', (event) => {
// Cancel the event as stated by the standard.
event.preventDefault();
// Chrome requires returnValue to be set.
event.returnValue = '';
});
Example 2: javascript beforeunload
window.addEventListener("beforeunload", function(event) { ... });
window.onbeforeunload = function(event) { ... };