0

我正在使用该功能

 window.onbeforeunload = function(){
  if(sessionStorage.hasOwnProperty("teststatred")){
            validNavigation = true;
            leave_message = "You are in the middle of the test, Are you sure do you want to close the window?"
        }
        if (validNavigation)
        {
            if (!e) e = window.event;              
            e.cancelBubble = true;
            e.returnValue = leave_message;               
            if (e.stopPropagation) {
                e.stopPropagation();
                e.preventDefault();
            }             
            return leave_message;
        }
 }

此功能将显示两个按钮的窗口:Leave this pageStay on this page。当用户单击或“留在此页面上”时,如何执行功能Leave this page

4

1 回答 1

0

不幸的是,您不能,因为唯一的方法是使用confirm(), alert(), or prompt()它们,它们都将被忽略:

“自 2011 年 5 月 25 日起,HTML5 规范规定在此事件期间可以忽略对 window.showModalDialog()、window.alert()、window.confirm() 和 window.prompt() 方法的调用。”

https://developer.mozilla.org/en-US/docs/Web/API/window.onbeforeunload?redirectlocale=en-US&redirectslug=DOM%2Fwindow.onbeforeunload

但是,它确实可能,所以尝试一下并没有什么坏处prompt

于 2013-12-24T04:38:52.620 回答