Links to javascript open window

I am having trouble understanding how to link to new browser windows after they open. As an example, if I created 3 new windows from the main (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 can I programmatically focus (or just link to) the three browser if the two browser is currently in focus?

+3
source share
1 answer

I would have an array of child windows in the parent window. Then, for each child window, there is a function that adds the child to the parent array of childWindow. This way you can have any 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 this:

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

}
+2

Source: https://habr.com/ru/post/1738051/


All Articles