单击一个按钮打开多个javascript弹出框



这是一个JavaScript问题。

我需要创建一个按钮,点击一次,就会打开5个子窗口(弹出框)。

一个框将在屏幕的左上角,一个在右上角,另一个在左下角,一个子在右下角,还有一个子在中央。每个框都有一个不同的URL。

当主窗口关闭时,所有子窗口也应关闭。

我只能做打开一个弹出窗口的代码-无法弄清楚如何编码所有5个按钮,点击一次打开,然后在父窗口关闭时关闭它们。

以下是我目前所拥有的:

<SCRIPT language="JavaScript">
function myPopup(URL) {
popupWindow = window.open(URL,'popUpWindow','height=500,width=500,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
}
</SCRIPT>
<a href="#" onclick="myPopup(this.href);return false">Open Pop-Up</a>

非常感谢任何能帮助我的人。


感谢所有帮助我的人。通过利用这里的反馈,我已经在这个项目上工作了一整天,并提出了一些有效的代码。我不知道如何将每个弹出窗口的url分配给数组,但至少它们能工作。

但是,当父窗口关闭时,我无法关闭所有弹出窗口。希望你能帮忙。这是新代码:

<script type="text/javascript">
/*Use Array - Open 5 new popup windows using onclick*/
function newWindow() {
 var winPop = new Array();
  winPop[winPop.length] = window.open('top_right.html','window1','scrollbars=no,width=235,height=155,left=1000,top=200,screenX=1000,screenY=200');
window.open('top_left.html','window2','scrollbars=no,width=235,height=155,left=325,top=200,screenX=325,screenY=200');
window.open('bottom_left.html','window3','scrollbars=no,width=235,height=155,left=325,top=600,screenX=325,screenY=600');
window.open('bottom_right.html','window4','scrollbars=no,width=235,height=155,left=1000,top=600,screenX=1000,screenY=600');
window.open('center_page.html','window5','scrollbars=no,width=235,height=155,left=660,top=450,screenX=660,screenY=450');
}
/*NOT WORKING FOR ME --Close all popups when parent window is closed*/
onbeforeunload = function() {
    for (var index = 0; index < winPop.length; ++index)
        winPop[index].close();
}
</script>
</head>
<body>
<div id="wrap"> <a href='' onclick='newWindow()'><img src='images/group_clicker.gif' border='0'></a> </div>
</body>

DEMO

将所有打开的窗口保存到一个数组中
注册beforeUnload事件,当它触发时,循环显示它们的弹出窗口close()'。

注意:我认为它是jsFiddle或Chrome,但每次点击不会打开超过1个弹出窗口。

由于对弹出窗口打开的限制,您无法可靠地控制打开的弹出窗口的位置。打开YUI的面板或jQuery(UI)的对话等窗口弹出窗口可能会很有用

function openPopup(howMany) {
    var popups = [];
    var temp;
    for (var index = 0; index < howMany; ++index) {
        popups.push(open('', '', 'height=500,width=500'));
        popups[index].document.write('popup ' + (index + 1) + ' of ' + howMany + '<br/>this will close on parent window close');
    }
    closeFunc = function() {
        for (var index = 0; index < popups.length; ++index)
            popups[index].close();
    };
    if (addEventListener)
        addEventListener('beforeunload', closeFunc, false);
    else
        attachEvent('onbeforeunload', closeFunc);
}

您可以使用for循环打开多个窗口。请确保为弹出窗口指定一个唯一的名称

function myPopup(URL) {
    for (var i = 0; i < 5; i++) {
        var popupWindow = window.open(URL, i, 'height=500,width=500,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
    }
}

编辑的答案:

你写了

function newWindow() {
 var winPop = new Array();
  winPop[winPop.length] = window.open('top_right.html','window1','scrollbars=no,width=235,height=155,left=1000,top=200,screenX=1000,screenY=200');
window.open('top_left.html','window2','scrollbars=no,width=235,height=155,left=325,top=200,screenX=325,screenY=200');
window.open('bottom_left.html','window3','scrollbars=no,width=235,height=155,left=325,top=600,screenX=325,screenY=600');
window.open('bottom_right.html','window4','scrollbars=no,width=235,height=155,left=1000,top=600,screenX=1000,screenY=600');
window.open('center_page.html','window5','scrollbars=no,width=235,height=155,left=660,top=450,screenX=660,screenY=450');
}

你需要的是

var winPop;
function newWindow() {
    winPop = [
        open(
            'top_right.html',
            'window1',
            'scrollbars=no,width=235,height=155,left=1000,top=200,screenX=1000,screenY=200'
        ),
        open(
            'top_left.html',
            'window2',
            'scrollbars=no,width=235,height=155,left=325,top=200,screenX=325,screenY=200'
        ),
        open(
            'bottom_left.html',
            'window3',
            'scrollbars=no,width=235,height=155,left=325,top=600,screenX=325,screenY=600'
        ),
        open(
            'bottom_right.html',
            'window4',
            'scrollbars=no,width=235,height=155,left=1000,top=600,screenX=1000,screenY=600'
        ),
        open(
            'center_page.html',
            'window5',
            'scrollbars=no,width=235,height=155,left=660,top=450,screenX=660,screenY=450'
        )
    ];
}

这是一个作用域问题(以及您实际上没有将窗口添加到数组:|),winPop需要是global,即函数之外的vard。

最新更新