Is there a way to determine that the browser window was closed?

前端 未结 8 435
感动是毒
感动是毒 2021-01-14 12:35

How is it possible to identify that the browser\'s close button was clicked?

8条回答
  •  难免孤独
    2021-01-14 13:27

    From the JavaScript Event Reference, the closest match appears to be the OnUnload event. However, this also catches navigation away from the page (and thus you don't want the functions to run if the user actually clicks on a link.)

    There is no guarantee that the server will receive the message that the browser window is closed. For example, the older method that involved creating a small pop-up window would be blocked by most modern browsers. Using AJAX might work, but a browser window may close before it attempts to connect.

提交回复
热议问题