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

标签 c# javascript jquery asp.net

我有一个带有 MasterPage 和内容页面的 ASP.NET Web 应用程序,当我单击 MenuItem 打开新的 aspx 页面时,会从 MasterPage 中进行操作。如果我想关闭新的页面浏览器选项卡,我想显示一个弹出窗口或对话框,提醒用户他正在关闭浏览器选项卡。我在新的 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>

因此,每次刷新页面时都不会显示此消息

Link here to the source

关于c# - 在 ASP.NET 中检测关闭浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16605064/

相关文章:

javascript - TinyMCE 外包被选元素

c# - 从 Silverlight 重定向到另一个页面

javascript - 使用 Web API Get() 正确格式化 JSON

javascript - BabylonJS 画线

javascript - 获取数组中超过 4294967295 个对象

php - 调用JS函数时销毁 session 变量

javascript - 如何在 jQuery 中淡入*部分*?

c# - 获取viewbag动态属性的值为null

c# - 通过 linq to xml 中的属性获取 Node

c# - TweetSharp SendTweetWithMediaOptions 示例