H_79
H_79

Reputation: 217

Detect Close Browser in ASP.NET

I have a ASP.NET web app with a MasterPage and contents page, from the MasterPage when I click a MenuItem to open a new aspx Page. if I want to close the new page browser tab, I want to show a popup or a dialog that alert the user that he is closing the browser tab. I used the following code in the new aspx page:

<script type="text/javascript">
    $(window).bind("beforeunload", function () {
        $(window).unbind("beforeunload");
        return confirm("Do you really want to close?")
    })
</script>

the problem is that if i press also other buttons than the browse closeTab the method works. i would like to know how can i avoid it.

thanx in advance.

Upvotes: 2

Views: 10356

Answers (1)

AAlferez
AAlferez

Reputation: 1492

Yes, you should use something like this:

<script language="JavaScript" type="text/javascript">
    window.onbeforeunload = confirmExit;
    function confirmExit() {
    return "You are about to exit the system before freezing your declaration! If you leave now and never return to freeze your declaration; then they will not go into effect and you may lose tax deduction, Are you sure you want to leave now?";
    }
    $(function() {
        $("a").click(function() {
            window.onbeforeunload = null;
        });
        $("input").click(function() {
            window.onbeforeunload = null;
        });
    });
</script>

So this message is not showing everytime you refresh the page

Link here to the source

Upvotes: 3

Related Questions