Catching a tab close event in web browser?

前端 未结 7 2379
我寻月下人不归
我寻月下人不归 2020-12-18 08:45

Is there any way of knowing if the user closes a tab in a web browser? Specifically IE7, but also FireFox and others as well. I would like to be able to handle this situatio

7条回答
  •  余生分开走
    2020-12-18 09:25

    onbeforeunload also gets 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 are trying to log out the user if they close the tab or browser window, you would end up logging them out everytime they click a link or submit the page.

提交回复
热议问题