Search code examples
javascriptonunload

Report the time spent on a page to the server


Is it possible to report the time spend on a page back to server when the visitor leaves the page by closing the tab or the browser? Using Javascript.

And it will work on Firefox, Google Chrome, Safari, Opera, and IE8 (IE7 and 6 are not important).

But it will work in any case; even while the browser is closing, or it is typed another address.


Solution

  • You can try windows.onbeforeunload but there is no guarantee that it will work in all cases.

    Anyway I suggest you use google analytics which has that feature among many others (I doubt you can do better than google!). It's free.