Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Shrinivassab
Copy link
Contributor

@Shrinivassab Shrinivassab commented Jun 24, 2025

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

@Shrinivassab Shrinivassab requested a review from a team as a code owner June 24, 2025 23:57
@Shrinivassab Shrinivassab requested review from sideshowbarker and removed request for a team June 24, 2025 23:57
@github-actions github-actions bot added Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed labels Jun 24, 2025
Copy link
Contributor

github-actions bot commented Jun 24, 2025

Preview URLs

(comment last updated: 2025-06-26 02:59:00)

@Josh-Cena
Copy link
Member

@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.`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Member

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)

Copy link
Contributor

@vinhill vinhill Jun 26, 2025

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.`
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

window/close: clarify when a window is opened by script
3 participants