If you want to fire a HTTP request when closing a browser or tab, you could use the unload event in the past. But this was always a 50:50 change that it works at all, because the browser aborted this if necessary.

Since a few months some browsers stop sending AJAX in the unload-event.

But there is a new safe way to fire a HTTP request, when it is terminated: sendBeacon

This method ensures that even if the window is unloaded, the HTTP request is sent safely. And it no longer blocks the unloading of the window.

navigator.sendBeacon(url, data);

//  The URL that will receive the data. Can be relative or absolute.

//  A ArrayBuffer, ArrayBufferView, Blob, DOMString, FormData, or URLSearchParams object containing the data to send.


window.addEventListener("unload", function logData() {
  navigator.sendBeacon("/log", data);

For more details: