besbello wrote:
Hi all,
I want to submit a form when the user closes the browser, to keep a
count of the number of users accesing my application at each moment. To
do this, I use the onbeforeunload() event, that calls a function when I
call form.submit() method. The problem is that I have to put an
'alert()' after this line, if I remove the 'alert()' line, things
donīt work. The submit() method seems to return before the form is
really submitted, and the browser get closed, so the form is never
really submitted. I donīt want to have this annoying 'alert()'. Does
anybody know another way to ensure submit really executes before
browser get closed?
Thanks a lot.
Just a suggestion: have you tried POSTing synchronously using an
XMLHTTPRequest? It seems like the most reliable JS-based option,
disregarding backward (and some sideways) compatibility.
And a note... disregarding backward or sideways compatbility is not
something I recommend. Regardless of the direction you choose to go,
have a backup such as the auto-expiring sessions database noted below.
There is no 100% reliable way to do what you're looking for, JS or no.
Some people open popups that do the job, but they are often blocked or
closed before they can get the job done, and they're irritating as
[snipped]. Some people use session databases where sessions are
considered expired after X minutes/seconds, but this is highly
inaccurate [best used as a fall-back] and might involve a lot of extra
server-side work. You might combine this with a periodic automatic
refresh of some kind (fifty ways to skin that cat) to keep sessions
alive.
Other software might be more reliable, but still run into
incompatibility and accuracy issues. A Flash or Java applet that keeps
the server aware of its state would work. Of course, the client machine
would have to have Flash or JRE installed and security settings
configured appropriately to allow them to communicate with your server
(i.e. via a TCP socket/connection).
For more information on XMLHTTPRequest, search the group archives at
Google and the group FAQ. Lots of great info.
For more info on Flash and Java... well, I wouldn't know. Go somewhere
else. ;)