views:

97

answers:

2

Have no idea how to make it :( I think javascript should be used, any suggestions?

+1  A: 

Something like this?

var startDate = new Date();
window.onbeforeunload = function() {
    var millSecsOnPage = new Date() - startDate;
    // ajax save this value
};
David Hedlund
OMG...I tihnk I'm too tired, think I need a rest :( Thanks!
pedro
A: 

You need to post from the client side two times:

  1. First window.onload - when the user started viewing the page, tough you might be able to do it internally on your PHP/ASP server code. This might get broken by cache.
  2. When the user leaves - this is a must window.onclose.

Just post back an empty post - you only need to know the IP and the lookup for the previous time recorded.

... or... use Google Analytics. 2 minutes of work.

Edit: I see d's code. This is the first answer I gave you - but be warned, that without JavaScript - this cannot be done.

Edit2: Instead of using window.load, the post/get should be done in the header or in the . That php "js" can also be an empty file - you just need it to record the event.

elcuco
I'm not sure that window.onload is necessarily the best event to start measuring from, I'd measure it from when the script is executed. If you have a picture that takes 30 seconds to load, should the user's visit not be considered to start until the picture has loaded? In general I'd say it should start as soon as the HTML is being rendered.
Andrzej Doyle
You are so right, the programmer should post as soon as <body> is opened, or inside the <head>. I totally agree.
elcuco