How to show confirmation alert before closing browser tab using JavaScript - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

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. 

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon