vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, March 21, 2012

show alert message to user before closing browser in asp.net


Introduction:
Show alert message to user when closing browser window
 Its basic need to alert a customer before closing browser. In vb.net we used to write the code in closing button but in asp.net we can't do like that, so we go for JavaScript to make same alert like in vb.net. This alert will display whenever user try to close the browser or move to the next page alert will be shown...
Below Code we have to write body tag on window before unload event call the script function..
<body onbeforeunload = "ConfirmClose(event)">

Script:
<script type="text/javascript">
    var msg = "Are you sure you want to leave close this page?";
    function ConfirmClose(e) {
        var evt = window.event ? event : e;
        if (evt == e) {
            if (!evt.clientY) {
                evt.returnValue = msg;
            }
        }
        else {
           if (evt.clientY < 0) {

                evt.returnValue = msg;
            }
        }
    }
    </script>

I hope you like …

0 comments:

Post a Comment