Home Asp.net Prompt Alert Before Page Leave in JavaScript or jQuery

Prompt Alert Before Page Leave in JavaScript or jQuery

109
0
Prompt alert box before leaving webpage using javascript in asp.net?
Prompt alert box before leaving webpage using javascript in asp.net?

Now here in this tutorial, I’ll explain how to prompt alert before page leave (user leaves the page) or close the webpage tan in javascript as well as jquery with example code. If you liked this tutorial after going through it, please feel free to check out our other tutorials as well because they are highly valuable in helping you become a better coder.

In my previous tutorials, I’d explained how to show alert message from code-behind, how to call javascript function from code-behind, confirm message box example using javascript and other more cracking tutorials on Asp.net, JavaScript, jQuery here.

Alert Before Page Leave In JavaScript

Following is the JavaScript Code to show alert before page leave or closing current window:

<script type="text/javascript">
    //By using JavaScript
    window.onbeforeunload = function () {
        return 'please save your setting before leaving the page';
    }
</script>

Alert Before Page Leave In jQuery

Following is the jQuery Code to show alert before page leave or closing current window:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
    //By using jQuery
    $(window).bind('beforeunload', function () {
        return 'please save your setting before leaving the page.';
    });
</script>

Show Alert Before Current Window Close – [.aspx]

Following is the complete HTML Markup code for .aspx page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Prompt alert before leaving webpage using JavaScript in Asp.net</title>
    <script type="text/javascript">
        //By using JavaScript
        window.onbeforeunload = function () {
            return 'please save your setting before leaving the page';
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
        <h4>Show alert message box before leaving webpage using JavaScript in Asp.net</h4>
        <div>
            close this tab to test live demo
        </div>
    </form>
</body>
</html>
Note: Here I’d used JavaScript method to prompt alert message but you can also use jQuery mehod if you like.

Example Result

Prompt alert box before leaving webpage using javascript in asp.net?
Prompt alert box before leaving webpage using javascript in asp.net?
Previous articleCall JavaScript Function from Code-behind in Asp.net C# Vb
Next articleWrite C# or Vb.net Code-behind Code In aspx Page Inline
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

13 − 5 =