使用 JavaScript 多次打开同一个窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4904727/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:18:25  来源:igfitidea点击:

Open the same window multiple times using JavaScript

javascript

提问by Ching Ling

I'm new to JavaScript and trying to learn. How can I open the same window multiple times using JavaScript? Also, it didn't work when I changed the function name.

我是 JavaScript 新手,正在努力学习。如何使用 JavaScript 多次打开同一个窗口?此外,当我更改函数名称时它不起作用。

Here is the function:

这是函数:

<script type='text/javascript'>
function window(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left = 650,top = 250');");
}
</script>

回答by Matt Eskridge

Try something like this:

尝试这样的事情:

var numWindows = 0;
var windows = new Array(100);
var parameters = "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left = 650,top = 250";

function openNewWindow(url) {
    numWindows += 1;
    var title = "Window #"+numWindows;
    windows[numWindows] = window.open(url, title, parameters);
}

To access the windows, do this:

要访问窗口,请执行以下操作:

windows[num]

where num is the id of the window. The first id is 1, the second is 2, and so on.

其中 num 是窗口的 id。第一个 id 是 1,第二个是 2,依此类推。

回答by pree

Use a random number to open a popup as many times as you want:

使用随机数根据需要多次打开弹出窗口:

function openPopUp (url) {
    var randomno = Math.floor((Math.random()*100)+1); 
    window.open(url,'PopUpWindow'+randomno,'scrollbars=1,menubar=0,resizable=1,width=850,height=500');
}

回答by Dr.Molle

The 2nd parameter of window.open() (here it's the timestamp) must be different, otherwise window.open() will load the new window inside the existing(opened before with the same name->that's what the 2nd parameter assigns to the window).

window.open() 的第二个参数(这里是时间戳)必须不同,否则 window.open() 将在现有窗口中加载新窗口(之前用相同的名称打开-> 这就是第二个参数分配给窗户)。

You can also use "_blank"as 2nd parameter for open()

您还可以使用“_blank”作为 open() 的第二个参数

回答by Soumya

Try omitting with id parameter, like so:

尝试省略 id 参数,如下所示:

<script type='text/javascript'>
    function window(URL) {
        window.open(URL,'','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left=650,top=250');
    }
</script>

回答by jmort253

First, I would strongly encourage you to abandon using eval. It's very unsecure and not good to use.

首先,我强烈建议您放弃使用 eval。它非常不安全且不好使用。

Second, you can accomplish this without eval like so:

其次,您可以像这样在没有 eval 的情况下完成此操作:

function openWindow(URL) {
    day = new Date();
    id = day.getTime();
    windowCol[id] = window.open(URL, id, 
        'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left = 650,top = 250');
}

And just to avoid confusion, even though "window" works as a function name, I'd suggest changing it to make the code more readable as it may get confused with the global window object.

并且为了避免混淆,即使“window”用作函数名称,我还是建议更改它以使代码更具可读性,因为它可能会与全局窗口对象混淆。

回答by sudhAnsu63

Try this.

试试这个。

<script type='text/javascript'>
function window(URL) {
window.open(URL, "_blank", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left = 650,top = 250');
}
</script>