Yes, you can send an AJAX request to window.onbeforeunload, but it is not guaranteed that you will receive a response before closing the browser so that you can send the request only, but not rely on reading the answer. If you need to get a response, you can send a synchronous request instead of asynchronous.
source
share