We can check for browser close event through Javascript.
Here is the sample code that checks for browser or tab close using Javascript

<html>
<head>
<title>Browser Close Check</title>
<script type=”text/javascript”>
window.onbeforeunload=confirmExit;
function confirmExit(){
return “Your information is not saved. Do u really want to close this page?”
}
</script>
</head>
<body>
<h4>Browser Close Checking</h4>
</body>
</html>

The event ‘onbeforeunload’ fires before the document is unloaded.
The document will be unloaded when the user navigates to a different URL or attempts to close the browser window.

After executing the above script, when we close the browser it will fire the ‘onbeforeunload’ event, which in turn calls the function ‘confirmExit’. This function will alert the specified custom message.

By this way we can customize the browser close event.

Advertisements