window.onbeforeunload 对话框中的拦截选择

发布于 2024-11-06 06:25:12 字数 494 浏览 6 评论 0原文

在我的网络应用程序中,如果用户离开当前页面而没有保存表单中的更改,则会打开弹出窗口以提醒他这一点。

对于弹出窗口,我使用了一些从代码隐藏(C#)注入的脚本代码:

var Confirm = true;
window.onbeforeunload = confirmClose;  
function confirmClose() 
{
   if (!Confirm) return;

   if(/*CHECK CHANGE CONDITION IS TRUE*/)
      { return " + WARN_message + "; }
} 

我需要拦截用户是否单击“取消”或“确定”按钮。

我尝试过:

var button_pressed = window.onbeforeunload = confirmClose;

但它总是返回 true。

如何获取按下了哪个按钮? 谢谢

in my web application if the user leaves the current page without having saved changes in the form a pop up window is opened to alert him about that.

For the pop up I use some scripts code injected from codebehind (C#):

var Confirm = true;
window.onbeforeunload = confirmClose;  
function confirmClose() 
{
   if (!Confirm) return;

   if(/*CHECK CHANGE CONDITION IS TRUE*/)
      { return " + WARN_message + "; }
} 

I would need to intercept whether the user click on cancel or ok button.

I tried like:

var button_pressed = window.onbeforeunload = confirmClose;

But it returns always true.

How can get which button was pressed?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2024-11-13 06:25:12

不可能。没有与按钮关联的事件。
您可以做的是通过在 onbeforeunload 的页面中设置一个值或一个 cookie 来查看用户是否回来,并在一段时间过去后测试它是否存在,

但会看到重复的 如何知道用户是否在 Javascript onbeforeunload 对话框上单击“取消”?< /a>

Not possible. There is no event associated with the buttons.
What you might be able to do was to see if the user came back by setting a value or perhaps a cookie in the page in the onbeforeunload and test if it is there after some time has passed

but see the duplicate Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文