window.close() and different browser implementations – it's complicated...
- Published at
- Updated at
- Reading time
To see how it works, have a look at the button below:
<button type="button" onclick="window.open('/popular-posts/')"> Open popular posts </button>
Pretty nifty, right?
Interestingly, using this method is more complicated, and it's not always closing the current browsing context. The reason for that is that you don't want random third-party scripts closing your open window. There have to be rules about it!
The HTML spec defines conditions when a script is allowed to close a tab/window via
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.
That's great, and now we're entering the grey zone of browser interpretations. It turns out that the browsers out there implemented the specification-based closing functionality in different ways.
And that's where I hand over to Eric Lawrence. Eric wrote a fascinating article about the different
It's a great and fascinating read the multiple available browsers; thank you, Eric!
- How to remove all event listeners from a DOM element
- How to prerender Tweets without using the official Twitter APIs
- How to use EventTarget as a web-native event emitter