如何在页面上记录访问持续时间?

时间:2013-08-02 15:36:19

标签: php javascript visitor-statistic

如何在页面上记录访问持续时间?

目前,im log pages只通过将IP,用户代理等插入数据库来点击。

我知道,我必须使用javascript和“onbeforeunload”,但我不知道下一步该做什么。

1 个答案:

答案 0 :(得分:1)

难以通过jsFiddle(看起来)测试它,但你可以做类似的事情:

function enter() {
    this.crono = new Date().getMilliseconds();
}

function leave() {
    this.crono = new Date().getMilliseconds() - this.crono;
    alert(this.crono);
}

window.onload = enter;
window.onbeforeunload = leave;

代替alert(this.crono);,您可以使用crono值启动对服务器的ajax调用。