2010-03-22 134 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