2016-03-01 37 views
0

我有一個HTML頁面,可以爲應用程序彈出一個全屏窗口。我想要做的是檢測彈出窗口何時關閉並重新打開。當JavaScript關閉並重新打開時,JavaScript可以檢測到一個彈出窗口?

var drawingwindow; 

function popup(url) 
{ 
params = 'width='+screen.width; 
params += ', height='+screen.height; 
params += ', top=0, left=0' 
params += ', fullscreen=yes'; 
params += ', resizeable=no'; 
newwin=window.open(url,'drawingwindow', params); 
if (window.focus) {newwin.focus()} 
return false; 
} 

然後,我觸發彈出從HTML按鈕

<input type="submit" name="submit" value="Submit" onclick="popup('hello.php')"/> 

我想要做的某種方式是檢查drawingwindow第一頁 - 如果它關閉 - 立即重新打開它。它是主畫布「drawingwindow」上的繪圖應用程序的一部分,它可以永久關閉繪圖窗口。

+0

大多數瀏覽器允許打開彈出式窗口只爲點擊。因此,您無法打開(或重新打開)它,而無需用戶執行操作(單擊)。 –

+0

window.children訪問子窗口,== 0如果沒有。 –

回答

1

最近瀏覽器安全性的改進意味着你不能做你想做的事情。

可以檢測到彈出已關閉:

var popupWindow; 
var pop = function() { 
    popupWindow = window.open("example.html"); // has to be on same domain as original file 
    // setting a popupWindow.onclose event doesn't work (it 
    // fires immediately). You can fake it by observing some 
    // known property of the popup window, such as its location: 
    var fakeOncloseEvent = window.setInterval(function() { 
     if (!popupWindow.location) { 
      // The popup was closed. 
      window.clearInterval(fakeOncloseEvent); // tidy up 
      pop(); // <-- But this won't work! 
     } 
    }, 250); 
} 

重新開放時間將在大多數瀏覽器阻止彈出窗口,因爲你只允許打開一個新的窗口,直接用戶操作的結果(即點擊)。 (你可以使用上面的方式引導用戶重新打開彈出窗口,也許通過突出顯示「打開彈出窗口」鏈接,但不能打開它。)

或者,您可以解決問題從另一端,並將代碼放在彈出窗口,以防止它關閉。但是,大多數的瀏覽器嚴重限制了你被允許在onbeforeunload事件做什麼 - 你幾乎僅限於扔了一個確認對話框:

window.onbeforeunload = function() { 
    // Just return the string, don't return a confirm() here. 
    // Various browsers will include their own additional text 
    // in the confirm dialog, and some may ignore your added text altogether. 
    return "Are you sure you want to close this beautiful popup?"; 
} 
相關問題