javascript 使用 window.open() 在特定时间间隔后打开新窗口

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

opening new windows after specifc interval of time using window.open()

javascripthtmlbrowserwindow.open

提问by Sangram Nandkhile

I am looking for javascript code which will open new tabs(windows) automatically after specific interval of time.

我正在寻找将在特定时间间隔后自动打开新选项卡(窗口)的 javascript 代码。

i have few websites over here, in this code which open automatically when i press the button on the html page.

我在这里有几个网站,在这段代码中,当我按下 html 页面上的按钮时会自动打开。

I want these websites to open after specific interval. like, 1st website will open when user will press button "Open Windows", 2 nd website after 1 minute and 3 rd websites after 2 minutes.

我希望这些网站在特定时间间隔后打开。例如,当用户按下“打开 Windows”按钮时,第一个网站将打开,1 分钟后将打开第二个网站,2 分钟后将打开第三个网站。

eg.

例如。

<html>
<head>
<script type="text/javascript">
function open_win() {
window.open("http://www.google.com")
window.open("http://www.yahoo.com")
window.open("http://www.bing.com")
}
</script>
</head>

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

</html>

Thank you,

谢谢,

sangram

桑格拉姆

回答by Martin Jespersen

In most modern browsers you are not allowed to call window.openprogramatically, like through a setTimeout.

在大多数现代浏览器中,您不允许以window.open编程方式调用,例如通过setTimeout.

The browser will simply ignore the window.openstatement if it is not inside a callstack that is initiated by a direct user interaction, for instance a mouse click event.

window.open如果该语句不在由直接用户交互(例如鼠标单击事件)启动的调用堆栈内,则浏览器将简单地忽略该语句。

The reason for this is that it is very annoying behavior - you probably won't find a single person who enjoys using a site that opens windows by itself.

这样做的原因是,这是非常烦人的行为——您可能找不到一个喜欢使用自行打开窗口的站点的人。

So: reconsider what you are trying to do, there is bound to be a better way - one where you can work with the browser/user and not against it/him/her :)

所以:重新考虑你正在尝试做什么,肯定会有更好的方法 - 你可以与浏览器/用户一起工作而不是反对它/他/她:)

回答by Mike Soule

function open_win() {
    window.open("x.com");
    setTimeout("window.open('y.com')",60000);
    setTimeout("window.open('z.com')",120000);
}

This should open x.com then after one minute y.com and after two it should open z.com.

这应该打开 x.com 然后在一分钟后 y.com 和两分钟后它应该打开 z.com。