I'm having some issues understanding how to reference new browser windows after opening them. As an example, if I created 3 new windows from a main one (index.html):
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();
How could I programmatically focus on (or just refer to) browser "three" if browser "two" is currently in focus?
asked Mar 22, 2010 at 15:53
minimalpop
7,06413 gold badges71 silver badges83 bronze badges
1 Answer 1
I would have an array of child windows in the parent window. Then, for each child window, have a function that adds the child to the parent's childWindow array. That way you can have an arbitrary number of child windows.
//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)
Set the focus like so:
//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();
}
}
answered Mar 22, 2010 at 16:02
Rodrick Chapman
5,5732 gold badges33 silver badges34 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
minimalpop
for the "window.parent.childWindows.push(window);" i keep getting "Cannot call method 'push' of undefined" even though i have declared the childWindows array in my parent window
Rodrick Chapman
Try window.opener.childWindows
lang-js