Is there any cross-browser JavaScript/jQuery code to detect if the browser or a browser tab is being closed, but not due to a link being clicked?
If you are targeting browsers that support it, you can use the Page Visibility API available in HTML5. It doesn't directly detect tab changes, per-say, but visibility changes. Which would include (but not limited to) tab changes.
close tab because you indicate the last page and it closes that. close application is when you use a desktop application. Thanks @Parth_Doshi. If you are working with the browser, it is better to use close tab and then kill process to kill the background processing of browser.
Use the Pin tab option to keep Prevent Close available in Chrome. To make this as painless as possible, I recommend pinning this website to your browser then moving the tab out of the way. To do that open Prevent Close, and then right-click the tab with your mouse. From the context menu select Pin tab.
If I get you correctly, you want to know when a tab/window is effectively closed. Well, AFAIK the only way in Javascript
to detect that kind of stuffs are onunload
& onbeforeunload
events.
Unfortunately (or fortunately?), those events are also fired when you leave a site over a link
or your browsers back button. So this is the best answer I can give, I don't think you can natively detect a pure close
in Javascript. Correct me if I'm wrong here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With