-
Notifications
You must be signed in to change notification settings - Fork 22.8k
Clarify script-closable conditions for Window.close() (Fixes #39154) #40065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Clarify script-closable conditions for Window.close() (Fixes #39154) #40065
Conversation
Preview URLs (comment last updated: 2025-06-26 02:59:00) |
…ub.com/Shrinivassab/content into 39154-window-close-clarify-updated-doc
@vinhill Does this look good to you? |
- Windows opened using {{domxref("Window.open()")}} | ||
- Windows opened via web content, such as links (`<a target="_blank">`) or forms (`<form target="_blank">`), without user modifier actions | ||
|
||
Windows opened by browser UI actions — such as right-click → Open in new tab, Ctrl+Click, Shift+Click, or middle-click — are not script-closable. Calling `close()` on such windows will result in an error message like: `Scripts may not close windows that were not opened by script.` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Windows opened by browser UI actions — such as right-click → Open in new tab, Ctrl+Click, Shift+Click, or middle-click — are not script-closable. Calling `close()` on such windows will result in an error message like: `Scripts may not close windows that were not opened by script.` | |
Windows opened by browser UI actions — such as right-click → Open in new tab, Ctrl+Click, Shift+Click, or middle-click — are not script-closable. Calling `close()` on such windows might result in a console warning like: `Scripts may not close windows that were not opened by script.` |
The spec doesn't mandate any indication of a failure. From a quick test, Chromium, Safari and Firefox show differently worded console warnings.
{{domxref("Window.open()")}} method, or on top-level windows that have a single history entry. If the window doesn't match these requirements, an error | ||
similar to this one appears in the console: | ||
`Scripts may not close windows that were not opened by script.` | ||
This method can only be successfully called on windows that are considered _script-closable_. This generally includes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method can only be successfully called on windows that are considered _script-closable_. This generally includes: | |
Windows are _script-closable_ if they were created by web content. This generally includes: |
I would prefer MDN to use the phrase created-by-web-content given this is what browser code and the specification uses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm slightly confused because isn't "created by web content" one of the sufficient conditions of "script-closable"? (Granted I don't know what "session history entries" entails if not history.length
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I just think "created by web content" is the important condition and a brief summary for the two items mentioned. The history check is rarely relevant.
Per my understanding, session history entry size
and history.length
is pretty much equal.
- Windows opened using {{domxref("Window.open()")}} | ||
- Windows opened via web content, such as links (`<a target="_blank">`) or forms (`<form target="_blank">`), without user modifier actions | ||
|
||
Windows opened by browser UI actions — such as right-click → Open in new tab, Ctrl+Click, Shift+Click, or middle-click — are not script-closable. Calling `close()` on such windows will result in an error message like: `Scripts may not close windows that were not opened by script.` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Windows opened by browser UI actions [...] are not script-closable
Unfortunately, it depends :).
If history.length <= 1
, the window is script-closable even if not created by web content. In addition, these browser UI actions are not part of the specification and whether such windows are script-closable is thus up to implementers. I.e.
- right-click -> open in new tab -> window.close() works
- right-click -> open in new tab -> navigate once -> window.close() likely fails.
I think this goes into details that users are generally not interested in. But we could state something like
Windows opened by browser UI actions ... are often considered not opened by web content. They are only script closable as long as their history length is one.
Description
Added detailed information and examples for the
window.close()
method, clarifying its behavior when called from different contexts (e.g., opened vs. not opened windows).Motivation
The previous documentation was unclear about when
window.close()
is allowed to close the current tab or window. These changes help developers understand browser restrictions and avoid unexpected behavior in their applications.Additional details
Updated with references to the latest HTML Living Standard and linked to relevant browser compatibility notes.
Related issues and pull requests
Fix #39154