浏览器允许卸载时进行多少活动?
我正在使用 JavaScript 来跟踪用户在卸载该页面后在该页面上的活动。考虑以下简化的虚拟脚本来模拟我在卸载时所做的事情:
$(window).unload(function() {
$.get("http://www.google.de/images/srpr/logo3w.png");
});
在这种情况下,图像 URL 用作跟踪数据的持有者。
关闭浏览器窗口时,某些浏览器(例如 Firefox 3)会请求该图像,而其他浏览器(例如 Firefox 6)不会加载该图像。
也许这不是应该做的事情;无论如何,只要我能够声明卸载事件的可靠性,我就愿意保留它。
有这方面的经验吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对此有一些经验,我会推荐一种稍微不同的方法,如下所示:
挑战是,如果您在卸载时进行 AJAX 调用,则应该使用同步模式。使用普通的异步模式,它可能根本不会成功(例如在 Chrome 中)。
但在这种情况下,使用图像的技巧同样可靠,因为通信只是一种方式。这适用于 GET,但如果您需要 POST 某些内容,那么同步模式是唯一的选择。
I have some experience with that and I would recommend a slightly different approach like this:
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.