首页 > 解决方案 > 我想看看访问者在我的页面上停留了多长时间

问题描述

我可以在访问页面时找到用户的开始时间,但没有得到结束时间。

我尝试在 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?';
});

标签: javascriptjquery

解决方案


最好的解决方案是使用 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);
};

推荐阅读