Record visitor's time on a webpage

Discussion in 'JavaScript' started by weknowtheworld, Apr 18, 2007.

  1. #1
    Hi there,

    How do you record how long a visitor has been viewing a webpage?

    Thanks for replying.. :)
     
    weknowtheworld, Apr 18, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    I don't record that by myself, instead I'm using Google Analytics who provides that and many many more useful info
    .
     
    ajsa52, Apr 18, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="javascript">
    	var seconds = null;
    	function timer( )
    	{
    		seconds++;
    		setTimeout( 'timer()', 1000 );
    	}
    	function tellUser( )
    	{
    		alert( 'Thanks for visiting, you viewed this page ' + seconds + ' seconds' );
    	} 
    </script>
    </head>
    
    <body onunload="tellUser();">
    
    <script language="javascript">window.onload=timer();</script>
    </body>
    </html>
    
    HTML:
    You could do it like that, only instead of alerts use ajax and php to update a database or write to a file
     
    krakjoe, Apr 19, 2007 IP
  4. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks krakjoe for such kind help... :)
     
    weknowtheworld, Apr 19, 2007 IP