Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much activity do browsers allow on unload?

I am using a JavaScript to track the activities of users on my page upon unloading that very page. Consider the following simplified dummie-script to simulate what I am doing on unload:

$(window).unload(function() {
    $.get("http://www.google.de/images/srpr/logo3w.png");
});

The image URL in that case serves as a holder for tracking data.

The image is requested in some browsers (e.g. Firefox 3) and isn't loaded in others (e.g. Firefox 6) when closing the browser window.

Probably isn't the way it should be done; anyhow I would like to hold on to it as long as I could make a statement on how reliable the unload-event is.

Any experiences on this?

like image 516
Morris Avatar asked Oct 10 '11 14:10

Morris


People also ask

What is the difference between unload and Beforeunload?

beforeunload event – the user is leaving: we can check if the user saved the changes and ask them whether they really want to leave. unload – the user almost left, but we still can initiate some operations, such as sending out statistics.

What triggers unload event?

onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.). Note: The onunload event is also triggered when a user reloads the page (and the onload event).

What is an unload event?

The unload event is fired when the document or a child resource is being unloaded. It is fired after: beforeunload (cancelable event) pagehide.


1 Answers

I have some experience with that and I would recommend a slightly different approach like this:

$(window).unload(function() {
    new Image().src = "http://www.google.de/images/srpr/logo3w.png?timestamp=" 
           + new Date().getTime();
});

The challenge is that if you are making an AJAX-call at unload, you should use synchronous mode. With normal async-mode, it may not succeed at all (for instance in Chrome).

But in this case, a trick using image is just as reliable because the communication is one way only. That works for GET but if you need to POST something then sync-mode is the only option.

like image 170
M.L. Avatar answered Sep 22 '22 02:09

M.L.