onbeforeunload Event in JavaScript
onbeforeunload Event in JavaScript
The onbeforeunload event fires when the document is about to be unloaded. It allows us to display a message in a confirmation dialog box to inform the user whether they want to stay or leave the current page. The default message that appears in the confirmation box varies in different browsers. It is used with body element.
Syntax:-
Its syntax is:- < anyElement onbeforeunload="my_script" > OR object.onbeforeunload= function(){my_script} OR object.addEventListener("beforeunload", my_script)