Forum Moderators: open
I've set up a page that records the times (in a database) users login to it (start time), and I wanted to be able to determine the times they left the page (leave time), so I used the javascript onUnload function along with an Ajax request. This is my script:
window.onunload = function () {
new Ajax.Request('end.php');
}
Where end.php is a php file with the code that submits the time info to the database.
This works perfectly in Firefox and IE7. The problem I have is that the AJAX doesn't seem to be working in Safari or Chrome, and the onUnload() function doesn't work in Opera. I'm not sure of how this works in IE6, I haven't been able to test it yet.
I need this to work in as many browsers as possible because it's important that leave times be recorded for this page.
Is there something wrong with the code I've written?
Is there another way to achieve what I am trying to do?
Thanks in advance!
Another way I know people deal with this is to ping the server periodically (every 5 seconds or so) to update the "last activity" time... You won't get it down to the exact second for when they leave, but you'll reliably be within 5 seconds of that time.
HTH :)
new Ajax.Request" that you are using Prototype? If so, you can use the periodicalExecuter [prototypejs.org] feature of that library. It would look like this:
new PeriodicalExecuter(function(pe) {
new Ajax.Request('end.php');
}, 5);
Otherwise, you can build it yourself with something like this:
function updateTime(){
new Ajax.Request('end.php');
}
setInterval ( "updateTime()", 5000 );