tags:

views:

56

answers:

2

I recently got a situation where I need to set a bit in a database from 1 to 0 on web page on close event and I found there is no such event but an unload event so I thought I can generate an AJAX call from it.

Now, when some one tries to close the browser, it should be closed instantly because of UE and if I will have AJAX request that will change a bit from 1 to 0 in database, it would probably be taking a second in my internet connection but some one on the other side can have slow connection and the browser will wait little longer prior to close. Am I right? Or the browser will be hidden and it will carry the AJAX request in the background?

Or if you think any other solution is available that would be helpful.

+1  A: 

You could try popping up an alert() dialogue after firing off the AJAX event. That'd keep the browser open until the user could acknowledge the alert. But other than that, there's nothing you can do to prevent the browser from shutting down before the AJAX goes through (or fires off at all).

Marc B
+1 for thinking differently, I liked the alert way.
Umair Ashraf
+5  A: 

That is not a reliable way to do this. It's not guaranteed that the browser will make the request and it can also potentially detrimental to user experience.

Here's a page with details on better ways to accomplish this:
http://ajaxpatterns.org/Heartbeat

NullUserException
+1 Agreed. Moreover, the client machine / browser could become disconnected from the network, the browser could crash, etc.
Tadmas
+1 for the wonderful link
Umair Ashraf