1

Recently, at work, i made a redirection to another site. The maintainers of the other site contacted me and told me that their window.close are not working anymore. Here is a simple explanation of the problem:

let's say site A has an <a href="http://siteB" target="_blank" rel="noreferrer"> tag to navigate to the home page of site B. In the home page of site B, when you use window.close(), the window is closed. Now,(still in the site B), when the user navigates to another page (let's say /about page), a window.close in this page will not work. Chrome shows this warning:

Scripts may close only the windows that were opened by it.

But in the <a> tag, when i replaced rel="noreferrer" by rel="opener", window.close works in any page of the site B. For rel="noreferrer", window.close only works in the first page of the redirection. If i understand well the warning message, it's because the /about page was not opened by the site A, that's why window.close will not work in that page. But my question is, why by just changing rel="noreferrer" to rel="opener", window.close now works everywhere in site B?

Alee
  • 740
  • 7
  • 19

1 Answers1

4

In documentation of rel in mozilla there's saying rel="noopener" or rel="noreferrer"

Creates a top-level browsing context that is not an auxiliary browsing context.

The window opened like that has a top-level context.

But for rel="opener"

Creates an auxiliary browsing context.

It means it's not a top-level.

And now for documentation of window.close() there is

This method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.

So for closing window by close() function the window shouldn't has top-level context.

fatm
  • 422
  • 2
  • 11
  • Thanks Fatima. Yours quotes for rel="opener" and rel="noreferrer" helped me to understand better. In the html specs (https://html.spec.whatwg.org/multipage/window-object.html#dom-window-close), there is this paragraph: "A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a top-level browsing context whose session history contains only one Document." It know make sense when i read your answer. – Alee Nov 01 '21 at 13:09