javascript打开窗口引用

标签 javascript browser window

我在理解如何在打开新浏览器窗口后引用它们时遇到了一些问题。例如,如果我从一个主窗口 (index.html) 创建了 3 个新窗口:

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

如果浏览器“二”当前处于焦点状态,我如何以编程方式关注(或仅引用)浏览器“三”?

最佳答案

我会在父窗口中有一个子窗口数组。然后,对于每个子窗口,都有一个函数将子窗口添加到父窗口的 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();
    }

}

关于javascript打开窗口引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2493660/

相关文章:

java - 不使用 JFrame 创建屏幕(窗口)

javascript - 所有函数表达式突然不被识别为函数

javascript - Angular 2 应用程序中的 Service Worker 实现

javascript - 单击 # 链接时网站不断导致回发

javascript - 在 Web 浏览器控制应用程序中从 C++ 取消脚本脚本执行

javascript - CSS new 'content-visibility' 属性是否会干扰脚本加载行为?

python - 如何使 IconView 中的图标均匀分布?

javascript - 覆盖 bootstrap.js 中下拉菜单的行为

javascript - 在浏览器窗口中检测另一个应用程序(包括百分比重叠)

window - xmonad - 打开一个窗口进入特定的瓷砖