Monday, August 24, 2015

How to show confirmation alert before closing browser tab using JavaScript

In this article I am going to explain how to show confirmation alert before closing browser tab using JavaScript

Description:

When users enter the data or update the data on webform but in between by mistake users click on close tab and lost the all enter/update data. After all user have to again open that particular form and again insert/update the data.
To avoid inconvenience I am going to show a pop up message when users click on close tab.

Implementation:
Add the below given script in head section of page (webform, Html).

<script type="text/javascript">
    window.onbeforeunload = function () {
        return 'Please save the changes before close the tab !!!';
    }
</script>

Complete HTML Markup of webform:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
    window.onbeforeunload = function () {
        return 'Please save the changes before close the tab !!!';
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <fieldset>
        <legend>JavaScript Example</legend>
        <h1>Show confirmation alert before closing browser tab using JavaScript </h1>
    </fieldset>
    </div>
    </form>
</body>
</html>

Run the project and check it.

 Demo:
How to show confirmation alert before closing browser tab using JavaScript

  In this article we have learn to how to show confirmation alert before closing browser tab using JavaScriptI hope you enjoyed this article. 

No comments:

Post a Comment