2013-05-17 117 views
2

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

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

的问题是,如果我也按其他按钮不是浏览closeTab的方法工作。我想知道如何避免它。

thanx提前。

+1

请检查此线程 的http://计算器。 com/questions/1997956/javascript-window-close-event-rather-than-unload-event-for-all-browsers –

回答

2

是的,你应该使用这样的事情:

<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

+0

它在刷新时显示消息。如何解决它?谢谢 !! – kevin