I want to distinguish whether the tab closed by clicking X or by another action (clicking a link, or a button which close the current tab).

In Firefox - I followed the stack and saw that the function closing the tab (not by X) is called: onxblDOMWindowClose.
BUT in chrome - both cases's stack looks the same.

Is there any way I can know how the user close the current tab ?

I've googled this question in many versions, but didn't find any answer/work direction.

Any help will be appreciated.

This is not a direct answer but how I would find out what event did it. As you know Chrome responds to "events" so I would have to monitor such events in some app or code. The good news is that CEL (link to follow) did release source code if you need to look how it was done. Noted link is http://blogs.fluidinfo.com/terry/category/browser-extensions/

But here's the thing. You can't be sure if the event was directly user created or say a system event like the OS was rebooted due to a pending update or other. I'm sure you can guess more scenarios that can close a tab where the user didn't click it.


Member Avatar for diafol

Can you share the scenario in mind where this is important? Perhaps contributors could offer an alternative.

I needed to know if the window/tab closed by script or by browser action (clicking X button, for instence).
Chrome events wan't helpful in this case. So I re-implement window.close() - to announce me that the function has been called, and then close the window (also relevant to tab).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.