views:

163

answers:

1

Hi,

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?

+1  A: 

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();
    }

}
Rodrick Chapman
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
minimalpop
Try window.opener.childWindows
Rodrick Chapman
Tested, it works.
Rodrick Chapman