开发者

How much activity do browsers allow on unload?

开发者 https://www.devze.com 2023-04-11 19:23 出处:网络
I am using a JavaScript to track the activities of users on my page upon unloading th开发者_JS百科at very page. Consider the following simplified dummie-script to simulate what I am doing on unload:

I am using a JavaScript to track the activities of users on my page upon unloading th开发者_JS百科at 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消