在ASP.NET中检测关闭浏览器

Detect Close Browser in ASP.NET

本文关键字:浏览器 检测 ASP NET      更新时间:2023-09-26

我有一个ASP。. NET web应用程序与母版页和内容页,从母版页当我点击菜单项打开一个新的aspx页。如果我想关闭新的页面浏览器选项卡,我想显示一个弹出窗口或一个对话框,提醒用户他正在关闭浏览器选项卡。我在新的aspx页面中使用了以下代码:

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

的问题是,如果我也按其他按钮比浏览closeTab的方法工作。我想知道我怎样才能避免它。

提前致谢。

是的,你应该这样做:

<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>

所以这个消息不会在每次刷新页面时显示

链接到这里的源代码