Can beforeunload/unload be used to send XmlHttpRequests reliably

后端 未结 3 1267
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 10:57

recently, I had the urgent requirement to give my server a notice, that a specific page of my webapp is about to get closed. \"Easy peasy\" I thought, beforeu

3条回答
  •  北海茫月
    2020-12-08 11:25

    The thing to keep in mind is that beforeunload started as an extension by Internet Explorer. Automatically, that makes it a second-class citizen on the web. There is no specification, and browser implementation varies. For example, Firefox only partially implements it by not displaying the string, only a generic message.

    Additionally, even when fully implemented, it does not protect against all possible unload scenarios, eg, the user has terminated the processor, the browser has crashed, or the computer has been turned off. Even ignoring these extreme scenarios, I suspect that it might be possible to configure your browser to ignore such requests.

    My feeling is that you shouldn't rely on this message to save you. If this web app is internal, I would suggest training them to use the Save or Close or whatever buttons instead of just closing the tab. If it's external, maybe look into automatic saving as the user does their thing?

提交回复
热议问题