Browsers typically don't allow a child tab/window to close its opener with script. And yet they do allow the child to alter the location of the opener tab/window.
This is not entirely accurate. If you read the quoted MDN text more carefully, it is actually referring to the opening of the parent
, not the relationship to its popup
. The actual restriction you are probably running into is:
- Scripts may only close windows were originally opened by script.
So in your specific example, the popup
was opened by a script, but the opener
was opened by a user, and therefore the opener
cannot be close
d by either itself or the popup
.
Imagine a scenario where there are two popups
- User-opened tab (cannot be
close
d)
- Popup 1 (
opener
is user-opened tab)
- Popup 2 (
opener
is popup 1)
Popup 2 can close
popup 1 in this scenario.
However, popup 1 cannot close
the user-opened tab because that violates the rule above.
In Chrome & Firefox, it does not matter whether the popup
s are same origin as their opener
s. The popup
can change the location
or close
the opener
as desired, subject to my first point above.
In IE11, the popup
can close
the parent
, but cannot change parent.location
unless it it meets Same Origin policy. (results in new popup if you try)
Also, I'd like to clarify that any opener
can close
its own popup
s or change their location
without restriction. Access to the popup
contents is still subject to Same Origin policy for security reasons.
This leads to a curious workaround whereby you can create a page that does nothing but close itself with script as soon as its loaded, and then change the location of the parent tab/window to this new page, effectively closing it. ... does this workaround effectively create a back door?
To answer your question directly, there is no such workaround. The linked example will produce the error:
Scripts may not close windows that were not opened by script.
Unless you are in a 2-popup situation as I explained above, in which case the popup
could just close
its opener
directly. The workaround would be unnecessary in that case though.