我有一个带有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之外的其他按钮,该方法也可以。我想知道如何避免它。
提前。
答案 0 :(得分:3)
是的,你应该使用这样的东西:
<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>
因此,每次刷新页面时都不会显示此消息