Detect Close Browser in ASP.NET

做~自己de王妃 提交于 2021-02-04 08:02:11

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/16605064/detect-close-browser-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!