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

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

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

8条回答
  •  野的像风
    2021-01-14 13:38

    There is usually an event which fires when the user leaves the page (unload?) - this may be browser specific.

    There is also a property, window.closed, which is set on the window object after its associated window is closed. This is typically used when you have a reference to a popup window.

    I'm not sure whether the unload event would be fired before or after the "closed" property is set - you'll have to experiment.

    It may be that you can't distinguish between a closed window and a browser which has gone to another site.

提交回复
热议问题