JS: how to save client exit time in my DB, when client exit my web site by clicking X on the right corner
I have to save in DB the times when users entering and exitin开发者_如何转开发g my site, with the enter is no problem, and so with exit by clicking my "log off" button,
but what with the situation that client exit by clicking X in the right corner? how to handle this? I tried use onUnload event but it works also when redirecting to other page in the site. onClose event doesn't work.
any ideas??
Thanks, sara, PHP and JS developer
As far as I know there is no standard way to check for the closing of the browser vs redirect to another page. Easiest thing to do would be to consider the user as logged out if there hasn't been any activity from the user in the past 20 min (or whatever you like).
There is no reliable way to do this. Consider other situations that might happen: 1. Client's browser crashes 2. Client's OS crashes 3. Client's internet connection breaks 4. Client's computer explodes
There is no way you can 'catch' these 'events' so the most reliable thing you can do is to do a timeout like nico suggests.
You can use JavaScript to send 'heartbeat' requests to your server as often as you like (too often and you will annoy people), and if you miss three heartbeats consider they left the page (or they home was bombed)
精彩评论