Trying to build a cute little php site statistics script, now I'm stuck with getting the average visiting time on site.

How to do that? I'm using JS badge.

HTTP is stateless, if that is the case how the server knows when the visitor navigates away? JS onunload?

Recommended Answers

All 2 Replies

To calculate the average time spend by user on the page you can get the time when user enter your site and when exits your site. When user login store time when user logout or close the window you can store end time.

To get time when user close the window use the event 'onBeforeUnload' on body tag of your page. Like this:

<html>
<head>
<script language="javascript" type="text/javascript"> 
function handleUnload() 
{ 
	alert("bye");
               //your code to calculate the time diffrence and send it to server
} 
</script> 

<body onbeforeUnload="handleUnload()"> 
<body>

</html>

onbeforeUnload? Never heard of it, is it universally supported in all modern browsers?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.