2

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.

4
  • I've used onbeforeunload to send AJAX requests for this reason, and it worked in the major browsers. I can't give you a definitive list of what I tested, but you should know that it's been done before. Commented May 21, 2013 at 11:08
  • I've used software that, in retrospect, may have had this feature. My recollection is that there were restrictions on which browsers could be used. It is dishonest, but I'd be inclined to go with a line that basically said. "Standard-compliant browsers required. You must use xx, yy or zz browser to complete this test. Other browsers cannot guarantee the privacy of your answers and as such, are not supported" Commented May 21, 2013 at 11:28
  • 1
    You can't implement something that is not supported by browsers (AJAX on unload). Instead of sending data on unload, you'd better periodically send data to the server. Then, even if one closes the browser using the task manager, or if (s)he unplugs the (network) cable, you still have some data. Commented May 23, 2013 at 14:39
  • @RichieHindle: Performing AJAX (or rather "SJAX", because you can only perform a synchronous request) on onbeforeunload is not a good idea, if there is a bit of a delay the browser might just ignore the function because this will freeze the UI until the website has finished processing the request which is not very user friendly. Commented May 23, 2013 at 14:40

2 Answers 2

1
+100

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That is probably the best way to do it. Thank you, peterfoldi. (Bounty will be awarded in a few hours when the system allows me to.)
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.