2

I have a series of links, which I want to open in a new window. Once a window is open and a new link is clicked it should reload the window with a new url.

It works fine by providing a specified target (name) to window.open for a series of websites, but somehow after the google search page has been opened it opens up another window.

When I click Open w3school and then Open React the behaviour is as intended. If I click Open google search and then another link it pops up in a new window.

Steps to recreate:

  1. Click Open w3school opens a new window (intended)
  2. Click Open React reloads the page in the opened window (intended)
  3. Click Open google search reloads the page in the opened window (intended)
  4. Click Open w3school opens a new window (not intended)

The code below illustrates the issue.

Does anyone have an explanation of this behaviour?

<html>
<body>

<p>Click the button to open a new browser window.</p>

<button onclick="openW3school()">Open w3school</button>
<button onclick="openGoogleSearch()">Open google search</button>
<button onclick="openReact()">Open React</button>

<script>
function openW3school() {
  window.open("https://www.w3schools.com", "mywindow", "popup");
}

function openGoogleSearch() {
  window.open("https://www.google.com/search?q=javascript", "mywindow", "popup");
}

function openReact() {
  window.open("https://reactjs.org/", "mywindow", "popup");
}

</script>

</body>
</html>

Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22
lasse.iver
  • 23
  • 4
  • this line is not clear to me: _"If I click Open google search and then another link it pops up in a new window."_ .. what do you mean by saying "and then another link it pops up in a new window"? that `window.open` statement will show a new browser window with the google url loaded... and then? – Diego D Feb 07 '23 at 12:28
  • 1
    I tried adding a "Steps to recreate" section to the question. Hope that makes it more clear. – lasse.iver Feb 07 '23 at 12:37
  • I can't reproduce your issue in Firefox or Chromium browser (Edge), all the pages are opened within the same pop-up (the one originally opened for w3schools) as expected. Unfortunately the code doesn't work in StackSnippet, you can check it at [jsFiddle](https://jsfiddle.net/x7k56wm1/). – Teemu Feb 07 '23 at 12:41
  • Using Chrome version 109 has the issue on my end. I can reproduce the issue by pasting the code in w3schools code editor. (https://www.w3schools.com/html/tryit.asp?filename=tryhtml_default_default – lasse.iver Feb 07 '23 at 12:45
  • How about the fiddle I've linked above, does it work correctly for you? (I don't have Chrome at hands right now.) – Teemu Feb 07 '23 at 12:47
  • I get a `google.com is blocked` in the fiddle you link above, when I click on `Open google search`. – lasse.iver Feb 07 '23 at 12:51
  • A pop-up blocker blocks a pop-up on your testing browser? Set it off for testing. As you can see, pop-ups aren't reliable, it's best to use tabs instead. – Teemu Feb 07 '23 at 12:53

2 Answers2

1

One possible explanation would be that some script in the opened page changes its name (window.name).

You can verify / deny that if you check window.name of the "stray / detached" popup window in the console; if it will not be mywindow then you know the cause.

You may also check window.opener - if it is null, then it could indicate that given page deliberately broke ties with your page that created the popup.

myf
  • 9,874
  • 2
  • 37
  • 49
1

Most probably it is due the fact that the Google search results page is sent with

cross-origin-opener-policy: same-origin-allow-popups; report-to="gws"

HTTP header (while the other two pages in your sample are not), what presumably tells browser "block ties with openers if they were on different domain (origin)".

According to Cross-Origin-Opener-Policy MDN page it should have the effect you describe for Google Chrome:

If a cross-origin document with COOP is opened in a new window, the opening document will not have a reference to it, and the window.opener property of the new window will be null.

It is strange that Firefox does not seem to respect that, although it is presented as compliant there.

myf
  • 9,874
  • 2
  • 37
  • 49
  • Thanks. That makes sense. I guess there's no way for me to get around that? – lasse.iver Feb 07 '23 at 13:23
  • I guess there might be some way **for you** (or tightly controlled audience) to get around that, involving either some local browser preferences overrides (hacks) or proxy on your local network messing with those headers (also hacks). But for unknown audience, I don't think there could be done much about that. – myf Feb 07 '23 at 13:32