Re: Digest for leo-editor@googlegroups.com - 4 updates in 2 topics

24 views
Skip to first unread message

Gérard Piette

unread,
Apr 1, 2026, 11:41:20 PM (5 days ago) Apr 1
to leo-e...@googlegroups.com




Félix <felix...@gmail.com>: Mar 31 04:52PM -0700

@viktor
 
that's weird: after reading this I thought we cant have self-closing
(unless some specific circumstances)
https://developer.mozilla.org/en-US/docs/Web/API/Window/close
 
but after trying this simple html (save it as an .html file and try it
yourself) I was surprised to see the window close under chrome in windows
10.

*<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8">
<title>Close Tab Test</title></head><body> <h1>Close Tab Test</h1>
<button onclick="window.close()"> Close this tab
</button></body></html>*
 
So I guess I'll add the command and see if it works when served from the
web...!
:)
 
thanks for inquireing and insisting on this question! :D
 
Félix
  

Félix <felix...@gmail.com>: Mar 31 05:53PM -0700

@viktor I'm getting inconsistent results, so I'll refrain from adding this
feature... :/
 
In the browser so CTRL+W and other shortcuts like CTRL+F4 for closing the
browser tab still are usable for that :)
 
Félix
 
On Tuesday, March 31, 2026 at 7:52:45 PM UTC-4 Félix wrote:
 

Experimenting a bit and finding inconsistent behaviour of cindow-close() command, GP5 answered my question:

Why does the HTML command window.close() works only the first time when activated by a button on-click command, and fails to close the browser window on subsequent viewing of the same html page? pressing F5 or reloading the page does not restore the initial tab or window command? This behaviour in FireFox browser.

GP5:

Firefox blocks scripts from closing windows/tabs that were not opened by script (window.open). On the first click it may work because the page was opened by a script (or the browser treated the first user gesture as allowing the close). After that, reloading or revisiting the same page makes it a user-opened/tabbed document Firefox then prevents window.close() for security/usability reasons. Also modern browsers require a direct user gesture for window.close() to succeed; even with a gesture, closing is only allowed for windows opened by script.

How to handle it:

  • If you need to programmatically close a window/tab reliably, open it with script (from the opener page):
    • opener: let w = window.open(url); then inside that page window.close() will work.
  • If your page must be able to close itself after a user action without being script-opened, you cannot rely on window.close() in Firefox—show an instruction to the user (e.g., "Please close this tab") or provide an alternative flow that uses a separate script-opened window.
  • For development/testing, check the browser console for messages like “Scripts may not close windows that were not opened by script”.

No browser settings expose a reliable way for arbitrary pages to override this restriction (it’s intentional).

Reply all
Reply to author
Forward
Message has been deleted
0 new messages