Intercept selection in window.onbeforeunload dialog

白昼怎懂夜的黑 提交于 2020-01-17 03:39:31

问题


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


回答1:


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?



来源:https://stackoverflow.com/questions/5963079/intercept-selection-in-window-onbeforeunload-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!