Question

I have a web server that generates questions for students of a particular subject. The web server needs to keep track of how much time each student has spent on a particular set of questions.

The web pages have a "Finished" button, which, when pressed, causes statistics to be sent to server.

However, I also want the web browser to send statistics if the student navigates away from the page or closes the browser window without pressing "Finished".

For this purpose, I have planned to have "onunload" or "onbeforeunload" send an Ajax request to the server with the relevant information. But apparently different browsers do not fully support these events, and also there are restrictions on what can be done in the event handlers. And, of course, I don't want the browse to freeze if the communication with the server fails.

So, I need some advice on the best way to do this.

Was it helpful?

Solution

If I wanted to be sure to handle all the "special events" I would send tick 'requests' from the webpage to the server. Granularity depends on the tracking requirements, the load, and whether it is an intranet or internet application; can be some seconds or even a minute. So you are tracking the time spent on the page even if the browser/os/network crashes.

OTHER TIPS

The best way to implement is, is to use period updates. This will pretty much guarantee you have some relevant data when the user disconnects in any way.

An implementation is pretty trivial, all tough you might have to refactor some of your logic to send out period updates instead of everything at once.

function sendStatistics()
{
    // ajax and what not
}

setInterval(function(){
    sendStatistics();
}, 1000);

An other way to make it work is to make your ajax call in beforeunload and make it synchronous. This will freeze the browser for duration of the call, and will also only work when navigating away or closing the browser, i don't recommend this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top