views:

32

answers:

2

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?

+2  A: 

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.

Darin Dimitrov
A: 

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 :\

gothy