请求在页面卸载时确认

Zyp*_*rax -1 javascript asp.net confirm

在您提出新问题时,在StackOverflow上,您输入了问题,如果您决定离开该页面,则会收到"您确定"的确认.

我想在我的ASP.Net应用程序中执行相同的操作:

用户必须填写调查问卷,并且可以选择临时存储他的答案.如果用户决定离开页面而不临时存储他的答案,我们想要弹出确认并要求用户存储他的答案.

两个问题:

  • 在ASP.Net中页面卸载之前显示确认弹出窗口的正确方法是什么?
    我知道beforeunload事件,但我不想让它成为一个大的javascript黑客.

  • 当用户点击"保存"按钮(无论如何都保存答案)时,我不希望确认启动

rah*_*hul 5

你必须写下这个动作

onbeforeunload活动

触发之前被卸载页面.

<HTML>
<head>
<script>
function closeIt()
{
  return "Any string value here forces a dialog box to \n" + 
         "appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
  <a href="http://www.microsoft.com">Click here to navigate to 
      www.microsoft.com</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)