我想为每个HTML文档添加一个脚本(JavaScript),向服务器发送两个消息:
- 页面已打开
- 页面将关闭(此消息包含页面打开的时间长度)
当文档正在加载(或加载完成)时,应发送打开消息。这是简单的部分。
当文档从浏览器视口中卸载时,应发送关闭消息。 (用户单击没有target="_blank"的链接;用户关闭浏览器选项卡/窗口;用户重新加载页面;用户退出浏览器;使页面消失的任何其他操作)
我尝试了以下方法:
//================================================================================
//Global Variables
//================================================================================
gl = {}; //container for global variables (1 scalar and 1 function)
gl.start = Math.floor(Date.now()); //timestamp of page-loading
//================================================================================
//function: Send message to server
//================================================================================
gl.sendData = function (action) {
var message = {
'href' : window.location.href,
'height' : $(window).height(),
'width' : $(window).width(),
'action' : action, //can be 'open' or 'close' (i.e. 'load' and 'unload')
'rand' : Math.random() //random number to outwit cache
};
if (action == 'close') {
//how long was the page open? [milliseconds]
message.duration = Math.floor(Date.now()) - gl.start;
};
window.alert(action); //debugging: show if gl.sendData is executed
$.ajax({
'url' : 'http://' + window.location.hostname + '/path/to/script.pl',
'data' : message,
'success' : function(){}, //not interested in server's answer
'error' : function(){} //errors will be ignored
});
};
//================================================================================
//Things to do as soon as page load is complete
//================================================================================
$(document).ready(function(){
//send message "page did open"
gl.sendData('open');
//add event-handler to send the message "page will close" when the page is closing
$(window).on("unload", function() {
gl.sendData('close');
});
});
页面打开时发送消息完全正常。但浏览器不发送关闭消息。
我发现了这些事实:
- "unload" 似乎是正确的事件。当页面关闭时,警报消息弹出。
- "beforeunload" 不起作用,因为它没有被触发(Safari on Mac,在单击导航到另一页的链接时)
- ajax 请求在页面加载时发送,所以看起来没问题。
- 当页面关闭时,ajax 请求不会向服务器发送数据。
我的问题:
有没有办法在文档从浏览器卸载的那一刻向服务器发送消息?
我想将页面显示的持续时间发送到服务器。还有其他方法吗?