我有一个带有 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>
因此,每次刷新页面时都不会显示此消息
关于c# - 在 ASP.NET 中检测关闭浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16605064/