Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect browser or tab closing

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?

like image 686
cometta Avatar asked Oct 08 '10 08:10

cometta


People also ask

Can browser detect tab change?

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.

Is closing a tab the same as closing the browser?

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.

How do I stop browser from closing?

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.


1 Answers

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.

like image 97
jAndy Avatar answered Oct 07 '22 05:10

jAndy