Capturing tab close event in web browser?

Is there a way to find out if a user closes a tab in a web browser? In particular, IE7, but also FireFox and others. I would like to be able to handle this situation from our asp code if the current tab containing our website closes.

+4
source share
7 answers

Attach the onbeforeunload event. It can execute code just before closing the browser / tab.

+2
source

onbeforeunload is also called on the following events -

* Close the current browser window. * Navigate to another location by entering a new address or selecting a Favorite. * Click the Back, Forward, Refresh, or Home button. * Click an anchor that refers the browser to another Web page. * Invoke the anchor.click method. * Invoke the document.write method. * Invoke the document.open method. * Invoke the document.close method. * Invoke the window.close method. * Invoke the window.open method, providing the possible value _self for the window name. * Invoke the window.navigate or NavigateAndFind method. * Invoke the location.replace method. * Invoke the location.reload method. * Specify a new value for the location.href property. * Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method. 

So, if you try to log out, if they close the tab or browser window, you end up registering them every time they click on the link or send the page.

+6
source

Does document.unload do this if you?

+1
source

If you need to know when a page is closed on the server side, it is best to periodically send the server from the page (for example, via XMLHttpRequest ). When pinging stops, the page closes. This will also work if the browser crashed, was interrupted or the computer was turned off.

0
source

As Eran Halperin suggested, use onbeforeunload . In particular, return the line and this line will be included in the confirmation dialog box, which will allow the user to choose whether to leave or not to leave the page. Each browser has text before and after the returned string, so you should test in different browsers to find out what the user will represent.

0
source

I am sure that the OP is asking from the context of the web page itself, but for any Firefox add-on developers who encounter this question, you can use the TabClose event. https://developer.mozilla.org/en/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removed

0
source

Santos is right, this event will fire in many other actions than just clicking the close button on your tab / browser. I created a small hack to prevent onbeforeunload from triggering by adding the following function to the finished document.

  $(function () { window.onbeforeunload = OnBeforeUnload; $(window).data('beforeunload', window.onbeforeunload); $('body').delegate('a', 'hover', function (event) { if (event.type === 'mouseenter' || event.type === "mouseover") window.onbeforeunload = null; else window.onbeforeunload = $(window).data('beforeunload'); }); }); 

In addition, before starting any of the events mentioned by Santos, you need to run this line:

 window.onbeforeunload = null; 

If you do not want the event to be fired.

And here is the final piece of code:

  function OnBeforeUnload(oEvent) { // return a string to show the warning message (not every browser will use this string in the dialog) // or run the code you need when the user closes your page return "Are you sure you want to close this window?"; } 
0
source

Source: https://habr.com/ru/post/1277208/


All Articles