2013-12-24 160 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