I want to send a ping to my server using an AJAX GET or POST, right after they close the window.
Is window.onbeforeunload
a good idea?
Yes, you can send an AJAX request in the window.onbeforeunload
but it is not guaranteed that you will get a response before the browser closes so you could only send the request but don't rely on reading a response. If you need to get a response you could send a synchronous request instead of asynchronous.
Darlin is right, window.onbeforeunload solves the problem not 100%. If it's appropriate - you can show an alert() after sending and ajax request, this will block the browser and your request should be finished, but there's not 100% solution to this problem from my experience :\
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With