2010-03-22 133 views
3

我有一些問題了解如何在打開它們後引用新的瀏覽器窗口。舉個例子,如果我創建了3樓新的窗口,從一主一(index.html的):javascript打開窗口引用

var one = window.open('one.html', 'one',"top=10,left=10,width=100,height=100,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no"); 

    var two = window.open('two.html', 'two',"top=100,left=10,width=100,height=100,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no"); 

    var three = window.open('three.html', 'three',"top=200,left=10,width=100,height=100,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no"); 
    two.focus(); 

我怎麼能編程專注於(或只是參考)瀏覽器的「三包」,如果瀏覽器「二」是目前焦點?

回答

2

我會在父窗口中有一個子窗口的數組。然後,對於每個子窗口,都有一個將子項添加到父項的childWindow數組的函數。這樣你就可以擁有任意數量的子窗口。

//In the 'Main' window 
var childWindows = new Array(); 

//In the child window 
function onload() 
{ 
    window.parent.childWindows.push(window); 

} 

window.attachEvent('onload', onload); 
//or 
window.load = onload 
//or with jQuery 
$(window).ready(onload) 

將焦點設置像這樣:

//In the parent 

childwindows[i].focus(); 

//In a child 

parent.childwindows[i].focus(); 

//or to focus on next child from within a child 
var len = parent.childwindows.length; 
for (var i = 0; i < len; i++) 
{ 
    if (parent.childwindows[i] && parent.childwindows[i] == window) //you might want some other way to determine equality e.g. checking the title or location property. 
    { 
     var n = (i + 1) % childWindows.length; 
     childwindows[n].focus(); 
    } 

} 
+0

用於 「window.parent.childWindows.push(窗口);」即使我已經在我的父窗口中聲明瞭childWindows數組,我仍然收到「無法調用未定義的方法」 – minimalpop 2010-03-22 16:58:32

+0

Try window.opener.childWindows – 2010-03-22 17:30:52

+0

經過測試,它可以正常工作。 – 2010-03-22 17:56:27