Can You Fire An Event In Javascript Before The User Closes The Window?
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?
Solution 1:
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.
Solution 2:
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 :\
Post a Comment for "Can You Fire An Event In Javascript Before The User Closes The Window?"