如何打开多个弹出窗口?

5
我有五个链接,每个链接都指向一个页面。
function TohokuImgPopup(url) { 
popupWindow = window.open(
                    url, 'popUpWindow'+randomno, 'height=246,width=228,left=0,top=0,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no')
            }

这是我正在使用的函数。对于5个链接,我有不同的函数,每个链接都会打开一个新窗口。但是我只能同时打开一个弹出窗口。我怎样才能打开多个弹出窗口?
3个回答

6
我找到了答案。
<script type="text/javascript">

$(document).ready(


function a1(url) { 
popupWindow1 = window.open(
                    url, 'popUpWindow1', 'height=250,width=234,left=0,top=0,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no')
            }

            function a2(url) { 
popupWindow2 = window.open(
                    url, 'popUpWindow2', 'height=250,width=234,left=0,top=0,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no')
            }

            function a3(url) { 
popupWindow3 = window.open(
                    url, 'popUpWindow3', 'height=308,width=299,left=0,top=0,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no')
            }
            }
</script>

<a href="JavaScript:a1('images/focus_img1.html');">focus 1</a>
<a href="JavaScript:a2('images/focus_img2.html');">focus 2</a>
<a href="JavaScript:a3('images/focus_img3.html');">focus 3</a>

这些链接将在单独的窗口中打开


2
难道没有可扩展的解决方案吗?为每个链接硬编码一个函数不利于长远发展。 - o12d10

2

我知道这个问题已经被问了很长时间,但是我回答一下,因为我在网上找不到清晰回答这个问题的内容。一个简单的方法是使用带有click()事件的链接。

$('body').on('click', 'a[data-popup]', function(e) {
  var date = new Date();
  var mSec = date.getTime();
  my_window = window.open($(this).attr('href'), "Popup"+mSec, "top=0,left=0,menubar=no,toolbar=no,location=no, height=600, width=800");
  e.preventDefault();
  my_window.focus();
});

使用date.getTime()(它返回自1970年1月1日以来的秒数,因此不可能有任何重复的返回值...)可以为每个新的弹出窗口创建一个新名称 :)

-1
以下代码将打开您想要的弹出窗口数量。
<html> 
<head>
<title></title>
<script type="text/javascript">
 function TohokuImgPopup(url) {
window.open(url,"windowName","windowFeatures")
window.open(url,"DifferentWindowName","windowFeatures")// different name for each popup
// create windows as much as you want to create
}
</script>
</head>

<body>
<form>
<input type="button" value="Open Windows" onclick="TohokuImgPopup()">
</form>
</body>

你需要确保每个窗口的名称都不同,否则最后一个弹出窗口将覆盖其前面的弹出窗口。结果就会只有一个弹出窗口。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接