最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling window.open() multiple times fails after the first time - Stack Overflow

programmeradmin4浏览0评论

I have a script that loops through multiple urls and opens them in a new tab. It used to work but now only opens the first one. There's even a w3schools test editor that supposedly opens multiple windows, and it also fails after the first one:

.asp?filename=tryjsref_win_open6

However, if I step through in debug mode and reset the focus to to original tab each time, it does open each window. So my question is, how do I open multiple windows(tabs) but keep the focus on my window with the original script? It used to do it that way, but now, as soon as a new tab is added, it gets the focus, and the script stops opening windows. Here's the full w3schools failing script:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to open multiple windows.</p>

<button onclick="myFunction()">Open Windows</button>

<script>
function myFunction() {
    window.open("/");
    window.open("/");
}
</script>

</body>
</html>

I have a script that loops through multiple urls and opens them in a new tab. It used to work but now only opens the first one. There's even a w3schools test editor that supposedly opens multiple windows, and it also fails after the first one:

https://www.w3schools./jsref/tryit.asp?filename=tryjsref_win_open6

However, if I step through in debug mode and reset the focus to to original tab each time, it does open each window. So my question is, how do I open multiple windows(tabs) but keep the focus on my window with the original script? It used to do it that way, but now, as soon as a new tab is added, it gets the focus, and the script stops opening windows. Here's the full w3schools failing script:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to open multiple windows.</p>

<button onclick="myFunction()">Open Windows</button>

<script>
function myFunction() {
    window.open("http://www.google./");
    window.open("https://www.w3schools./");
}
</script>

</body>
</html>
Share Improve this question asked Aug 24, 2018 at 15:47 DShultzDShultz 4,5415 gold badges36 silver badges55 bronze badges 7
  • Don't omit the "window name" argument, use e.g. "_blank" as the name, or give an unique name for every window. – Teemu Commented Aug 24, 2018 at 15:54
  • 1 I believe chrome has a security feature which needs at least one user action per window.open, I might be wrong. – Daniel Sharp Commented Aug 24, 2018 at 15:55
  • 1 Firefox opened two windows for me; Chrome and Safari only opened the first one. Chrome additionally shows a "Popup blocked" message, allowing me to disable that security feature; @FZs is it possible you've already disabled it? – Daniel Beck Commented Aug 24, 2018 at 16:20
  • 1 @DanielBeck Yes, I checked, and it is enabled. When I disabled them, only Google opened... – FZs Commented Aug 24, 2018 at 16:30
  • @FZs yeah, "Enabled" means you've enabled popups, i.e. disabled the security feature (of blocking popups). Weirdly confusing UI on Chrome's part, there.... – Daniel Beck Commented Aug 24, 2018 at 16:35
 |  Show 2 more ments

3 Answers 3

Reset to default 3

As noted in another answer and the ments, it is a security feature.

It is worth adding, however, that you can disable it (on a per-site basis): simply click the small "popup blocker" icon at the right end of the address bar and enable popups from the current site.

Afterwards, feel free to shoot as many window.open's as you need.

I think as @Daniel Sharp said, there is a security feature in chrome. It works fine in FF and IE.

It works if I follow below steps:

  1. Click Open Windows (A new window will open)
  2. Click on inital browser tab
  3. Click somewhere on the page (Except Open Windows button)
  4. Click on Open Windows again (IT works this time - two windows are open)

The window name should be unique. Otherwise, the same page will refresh

function myFunction() {
    window.open("https://www.google./","_google","height=200,width=150");
    window.open("https://www.w3schools./","_w3schools","height=200,width=150");
}
发布评论

评论列表(0)

  1. 暂无评论