Home > Web Front-end > JS Tutorial > Why Won't `window.close()` and `self.close()` Close My Chrome Window?

Why Won't `window.close()` and `self.close()` Close My Chrome Window?

Barbara Streisand
Release: 2024-12-14 19:01:14
Original
154 people have browsed it

Why Won't `window.close()` and `self.close()` Close My Chrome Window?

"window.close and self.close Do Not Close the Window in Chrome: A Comprehensive Explanation and Solution"

Contrary to popular belief, JavaScript is indeed capable of closing windows created by scripts, even in Chrome. This security measure, introduced to prevent malicious activities, restricts JavaScript from closing windows not created by the same script.

The Root of the Problem

According to the window.close() specification, a browser may choose to close a window if the following conditions are met:

  1. The browsing context must be "script-closable," meaning it was created by a script or has only one document in its session history.
  2. The browsing context of the incumbent script must be familiar with the target browsing context.
  3. The browsing context of the incumbent script must be allowed to navigate the target browsing context.

Firefox Restriction

Firefox strictly enforces the condition that JavaScript can only close windows opened by the same script. Any attempt to close a window that does not meet this criterion will result in the error message, "Scripts may not close windows that were not opened by script."

Chrome's Exception

Chrome allows an exception to this rule, allowing JavaScript to close windows even if they were not created by the same script. This exception does not apply to userscripts, however.

Workarounds

Chrome

  1. Self Redirection Exploit (Deprecated): Replace the current page with itself and then close the window: open(location, '_self').close();. This exploit is now mostly blocked.
  2. TamperMonkey Override (Limited Functionality): Using an explicit // @grant directive and calling window.close() can close additional tabs in Chrome version 43 and above.

Firefox

  1. Security Setting Tweak (Not Recommended): Disable the security protection by setting allow_scripts_to_close_windows to true in about:config. Note that this poses a security risk.

Long-Term Solution

For reliable window closing, consider developing a Chrome extension or Firefox add-on. These extensions can close the current window without the limitations of JavaScript.

The above is the detailed content of Why Won't `window.close()` and `self.close()` Close My Chrome Window?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template