javascript - 我想看看访问者在我的页面上停留了多长时间
问题描述
我可以在访问页面时找到用户的开始时间,但没有得到结束时间。
我尝试在 jquery 事件下获取用户离开页面的时间,但它对我不起作用。你能帮我得到访客的结束时间吗?
$(document).ready(function() {
$(window).unload(function() {
alert("you are leaving from page");
});
});
</script>
$(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
});
解决方案
最好的解决方案是使用 navigator.sendBeacon:
var navigator.sendBeacon = navigator.sendBeacon || function (url, data) {
var client = new XMLHttpRequest();
client.open("POST", url, false); // third parameter indicates sync xhr
client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
client.send(data);
};
推荐阅读
- java - 可以将 maven-javadoc-plugin 配置为不包含生成日期吗?
- python - 在水平堆积条形图中对齐值标签 (Matplotlib)
- websocket - 在标头中传递 jwtToken 时,Spring WebSocket 无法向经过身份验证的用户发送消息
- c++ - 如何使用 ArduinoJson 从同一个文件中检索多个 NDJSON 对象?
- java - java.lang.NoClassDefFoundError:无法初始化类 com.google.pubsub.v1.ProjectTopicName
- javascript - 如何将对象映射到对象
- java - 静态到非静态重构,不能两者兼得?
- sql-server - SQL Server 忽略凝胶符号
- mysql - 在mysql表中查找下一个/上一个数字
- firebase - Flutter MissingPluginException(在通道 plugins.flutter.io/firebase_admob 上找不到方法 loadInterstitialAd 的实现)