在ASP.NET中检测关闭浏览器 [英] Detect Close Browser in ASP.NET

查看:66
本文介绍了在ASP.NET中检测关闭浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有MasterPage和内容页面的ASP.NET Web应用程序,当我单击MenuItem打开一个新的aspx页面时,该页面就来自MasterPage.如果我想关闭新页面的浏览器选项卡,我想显示一个弹出窗口或对话框,提醒用户他正在关闭浏览器选项卡.我在新的aspx页面中使用了以下代码:

 < script type ="text/javascript">$(window).bind("beforeunload",function(){$(window).unbind("beforeunload");返回确认(您真的要关闭吗?")})</script> 

问题是,如果我也按下浏览closeTab以外的其他按钮,则该方法有效.我想知道如何避免这种情况.

提前谢谢.

解决方案

是的,您应该使用这样的内容:

 <脚本语言="JavaScript" type ="text/javascript">window.onbeforeunload =确认退出;函数confirmExit(){return您将在冻结声明之前退出系统!如果您现在离开并且永远不返回以冻结声明,那么它们将不会生效,并且您可能会失去税收减免,您确定要立即离开吗?";}$(function(){$("a").click(function(){window.onbeforeunload = null;});$("input").click(function(){window.onbeforeunload = null;});});</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.

解决方案

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

这篇关于在ASP.NET中检测关闭浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆