3

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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
Try window.opener.childWindows

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.