javascript 如何打开多个弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18816343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open multiple pop-up windows?
提问by Gravity M
I have five links and each links to a page.
我有五个链接,每个链接都指向一个页面。
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')
}
This is the function I am using. I have different function for the 5 links, each opens a new window. But I am only able to open one popup at a time. How can I open multiple popups?
这是我正在使用的功能。我对 5 个链接有不同的功能,每个链接都会打开一个新窗口。但我一次只能打开一个弹出窗口。如何打开多个弹出窗口?
回答by Gravity M
I found the answer.
我找到了答案。
<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>
These links will be opened in separate windows
这些链接将在单独的窗口中打开
回答by Olivier Bérubé
I know it's been a long time since the question was asked, but i'll answer because I found nothing on the web that clearly answer the question. An easy way is to use a link with an click() event.
我知道自从提出这个问题已经很长时间了,但我会回答,因为我在网上找不到任何可以明确回答问题的内容。一个简单的方法是使用带有 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();
});
using the date.getTime() (It is impossible to have any return value repeated since it's the amount of seconds since 1970 january 1st...) makes each new popup window with a new name :)
使用 date.getTime() (不可能重复任何返回值,因为它是自 1970 年 1 月 1 日以来的秒数......)使每个新弹出窗口都具有新名称:)
回答by Raihan Ridoy
The following code will open the number of popups as you want
以下代码将根据需要打开弹出窗口的数量
<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>
you need to make sure that each window name is different, otherwise the last popup will overwrite it's previous popup. As a result you will end up with a single popup
您需要确保每个窗口名称都不同,否则最后一个弹出窗口将覆盖它之前的弹出窗口。结果你会得到一个弹出窗口