22 Sept 2015

How to Capture browser close button event or exiting the page in JavaScript

Capture browser close button event or exiting the page in JavaScript:

=============================================

This is a common use case. You want to capture the browsers close event so that you can alert user if there is any unsaved data on webpage that should be saved. Below javascript snippet will help you in that.

<script language="javascript">
function fnUnloadHandler() {
       // Add your code here
       alert("Unload event.. Do something to invalidate users session..");
}
</script>
<body onbeforeunload="fnUnloadHandler()">
    <!-- Your page content -->
</body>
Share:

0 comments:

Post a Comment