Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday, 9 July 2015

Introduction:

In this article I will explain how to confirm before closing or leave page (go back or forward). While working in asp.net we face this issue to check or confirm whether user wants to leave page or not. It can be done by using Javascript or jQuery Script.


Following Javascript code is used to confirm whether user wants to leave page or not:

<script type="text/javascript">
        var hook = true;
        window.onbeforeunload = function () {
            if (hook) {
                return "Did you save your stuff?"
            }
        }
        function unhook() {
            hook = false;
        }
    </script>
We confirm on   window.onbeforeunload event.

Source Code:

Full Code for Sample webpage:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        var hook = true;
        window.onbeforeunload = function () {
            if (hook) {
                return "Did you save your stuff?"
            }
        }
        function unhook() {
            hook = false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
        <strong>Alert before close?
    </strong>
    </div></center>
    </form>
</body>

</html>
Categories: , ,

0 comments:

Post a Comment