|
Sunday, 17 June 2007 |
This script can tell the visitors of your homepage
how much time they have spent on your page. To do that, first we create
a function called person_in(). This function creates a new
date instance which is called via the onLoad() event handler.
We then create another function called person_out() which
is called via onUnload() event handler. This function also
creates a new date instance. We then take the difference of the two
date instances, divide the result by 1000, and round the result. The
reason to divide the result by 1000 is to convert the visited time into
seconds. The result is then displayed via an alert() method.
Measuring a user's time on a page
<HTML> <HEAD> <TITLE>Detecting User's Time on a Page</TITLE> <SCRIPT LANGUAGE="JavaScript"> function person_in() { enter=new Date(); } function person_out() { exit=new Date(); time_dif=(exit.getTime()-enter.getTime())/1000; time_dif=Math.round(time_dif); alert ("You've only been here for: " + time_dif + " seconds!!") } </SCRIPT> </HEAD> <BODY bgcolor=ffffff onLoad='person_in()' onUnLoad='person_out()'> </BODY> </HTML> |