Home >Web Front-end >JS Tutorial >Why Won't `window.close()` and `self.close()` Close My Chrome Window?
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.
According to the window.close() specification, a browser may choose to close a window if the following conditions are met:
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 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.
Chrome
Firefox
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!