Can you fire an event in JavaScript before the user closes the window?

I want to send ping to my server using AJAX GET or POST, right after closing the window. Is a window.onbeforeunloadgood idea?

+5
source share
2 answers

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.

+5
source

, window.onbeforeunload 100%. - () ajax, , , 100% - :\

+2

All Articles