Saturday, January 28, 2012

Measuring users time on a page using JavaScript

 JavaScript Code:
<script language="javascript" type="text/javascript">

    function personIn() {
        EnterDate = new Date();
    }

    function personOut() {
        ExitDate = new Date();
        timeDiffer = (ExitDate.getTime() - EnterDate.getTime()) / 1000;
        timeDiffer = Math.round(timediffer);
        alert("You have only been here for: " + timeDiffer + " seconds!!");
    }

</script>

HTML Code:
<form id="formId" title="TitleHere" onLoad='personIn()' onUnLoad='personOut()'>
</form>

Show a digital clock in the status line
JavaScript Code:
<script language="javascript" type="text/javascript">

    function displyTimeinStatusLine()
    {
        var dat = new Date();
        var hr = d.getHours();
        var month = d.getMinutes();
        var AMPM = (hr >= 12) ? "PM" : "AM";
        if (hr > 12) h -= 12;
        if (hr == 0) h = 12;
        if (month < 10) month = "0" + month;
        var total = hr + ':' + month + ' ' + AMPM;
        defaultStatus = total;

        setTimeout("displyTimeinStatusLine()", 50000);
    }
</script>

HTML Code:
<body onload="this.displyTimeinStatusLine()">
    My contents here...
</body>

No comments: