Search code examples
javascriptwindow.open

window.open with name - google 'search results' page behaves different


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>


Solution

  • 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.