window.onbeforeunload 对话框中的拦截选择
在我的网络应用程序中,如果用户离开当前页面而没有保存表单中的更改,则会打开弹出窗口以提醒他这一点。
对于弹出窗口,我使用了一些从代码隐藏(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可能。没有与按钮关联的事件。
您可以做的是通过在 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?