Browser agnostic hook to send asynchronous request upon window.onunload

225 Views Asked by At

When a user closes a browser tab or window we want to implement a hook to send an http delete request.

My question is - can

window.onunload

or

window.onbeforeunload

guarantee the sending of AJAX requests or is there no such guarantee?

Now, we don't need to handle the http response, we just need a high success rate of delete requests fulfilled which means we need to guarantee the requests get sent before the runtime shuts down.

Also this would have to work across major browsers.

2

There are 2 best solutions below

1
c-smile On

No one system/browser can give you such a guarantee.

And yet wireless signal lost for example also qualifies as "session ended" I think.

The only option is to use some sort of heartbeat signals from client to server.

0
Blackbingo Yan On

Navigator.sendBeacon could be useful in your case, but it can only send POST request currently.