Forum Moderators: open

Message Too Old, No Replies

Using AJAX with onUnload()

         

ajcether

5:50 pm on Mar 26, 2009 (gmt 0)

10+ Year Member



Hi there,

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!

whoisgregg

9:18 pm on Mar 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it's not considered reliable to expect an onunload event to trigger. Particularly when you are dealing with a server request, it may be cancelled before it completes.

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 :)

ajcether

9:31 pm on Mar 26, 2009 (gmt 0)

10+ Year Member



Could you explain to me how to do this?

Thanks for your help!

whoisgregg

11:04 pm on Mar 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm going to guess from the "
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 );

ajcether

1:02 pm on Mar 27, 2009 (gmt 0)

10+ Year Member



It looks like this is going to work for me, thanks so much for your help! I really appreciate it :)

Fotiman

2:03 pm on Mar 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's important to note that your usage data will not be correct for those users with JavaScript disabled.

ajcether

2:30 pm on Mar 27, 2009 (gmt 0)

10+ Year Member



Yes, I'm aware of this...there's not much I'm going to be able to about it though... I don't know of any way to do what I'm trying to do without using Javascript :)

I'm not too worried about this either...the majority of the users that will be using the site should have Javascript enabled.

whoisgregg

5:37 pm on Mar 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Happy to help. :)