2013-12-24 162 views
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或'留在本页面上'执行功能?

回答

0

不幸的是,你不能,因为只有这样,是使用confirm(), alert(), or prompt(),他们都将被忽略:

“自2011年5月25日,即调用window.showModalDialog HTML5规范状态() ,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东西。

+0

我们怎样才能找到按钮“leavethispage”和“stayonthispage”,我们如何分配点击事件 – Jonathan

+0

@ DoorknobofSnow好,编辑。 – Cilan