2011-05-17 49 views
4

我有一個網頁,其中包含四個鏈接,每個鏈接打開一個彈出窗口。我使用的是常見的JavaScript函數來打開彈出式窗口,下面如何將焦點設置到子窗口而不刷新它?

function OpenPopup(pageURL, title,w,h) { 

    var left = (screen.width/2)-(w/2); 

    var top = (screen.height/2)-(h/2); 

    var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, 
     directories=no, status=no, menubar=no, scrollbars=Yes, resizable=no, 
     copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left); 

    targetWin.focus(); 

} 

給出當我點擊一個鏈接它會打開一個彈出窗口,其中包含一個數據輸入形式,我輸入了一些信息轉換成表格。然後我需要查看其他彈出窗口中的其他信息,所以我回到我的主窗口,單擊另一個鏈接,一個新的彈出窗口將打開,其中包含我需要查看的信息。我得到信息並關閉了這個彈出窗口,所以現在我在我的主窗口上,然後再次點擊該數據輸入窗體的鏈接,所以彈出窗口出現了,但是我輸入了我輸入的信息,這是因爲我再次打開彈出式窗口,新的彈出式窗口將用相同的名稱替換現有的彈出式窗口。

現在我的問題是:

  1. 是他們的任何方式,我可以檢查出是否彈出窗口已經從父窗口打開?

  2. 如果彈出窗口已經打開,我怎樣才能將焦點設置到它,而不是重新加載它?

  3. 如何從父窗口引用子窗口,而不需要通過子窗口的名稱存在子窗口的對象?

  4. 即使在刷新父窗口之後,是否可以從父窗口引用子窗口?

+0

我的建議是完全跳過彈出窗口。大多數瀏覽器會阻止它們,假設它們是廣告。無論如何,它們都是糟糕的設計。在應用程序中顯示/隱藏div,向下滑動這些表單的其餘內容。 – HyderA 2011-05-17 14:42:21

回答

0

我想你想要的1和2:

if(!targetWin) 
{ 
    var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, 
     directories=no, status=no, menubar=no, scrollbars=Yes, resizable=no, 
     copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left); 
} 
targetWin.focus(); 
+0

我認爲他希望能夠打開多個窗口,而不是阻止訪問現有的窗口。 – HyderA 2011-05-17 14:43:59

+0

沒有我需要的是將焦點設置到已打開的窗口,而不刷新它,我得到了解決方案,您可以在http://ektaraval.blogspot.com/2011/05/how-to-set-focus- to-child-window.html – Ekta 2011-05-19 14:42:36

5

爲1和2,您可以使用此功能

// loads an URL into the popup without reloading it 
function popupnr(mylink, windowname, refocus) { 
    // open the window with blank url 
    var mywin = window.open('', windowname, 'window attrs here'); 

    // if we just opened the window 
    if (mywin.closed || (!mywin.document.URL) || (mywin.document.URL.indexOf("about") == 0)) { 
     mywin.location = mylink; 
    } 
    else if (refocus) { 
     mywin.focus(); 
    } 

    // return the window 
    return mywin; 
} 
  1. 你不能指代一個孩子窗口的名字。沒有window.children。您需要致電window.open以獲得參考。

  2. 是的,你可以。但你需要使用同一窗口的名稱,同時呼籲window.open

+0

感謝Aravindan,這就是我正在尋找的東西... – Ekta 2011-05-19 14:40:38

+0

請「接受」答案..點擊答案旁邊的勾號。 – 2011-05-24 18:14:10

相關問題