XMLHttpRequest跟踪问题(仅适用于Firefox和Safari)

时间:2016-07-05 11:05:06

标签: javascript php xmlhttprequest tracking issue-tracking

我正在使用系统来跟踪用户活动。它就像谷歌Chrome上的魅力一样,但它不适用于Firefox和Safari(我猜是由window.close引起的),因为它没有写出' end_start'在我的数据库中,页面结果仍然打开:

jsHandler.js

function start(pageName) {
    var xmlhttp3;
    if (window.XMLHttpRequest) {
        xmlhttp3 = new XMLHttpRequest();
    } else {
        xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp3.onreadystatechange=function() {
    if (xmlhttp3.readyState==4 && xmlhttp3.status==200) {

    }
}

xmlhttp3.open("GET","includes/trackStart.php?pageName="+pageName,true);
    xmlhttp3.send();
}


function end() {
    var xmlhttp3;
    if (window.XMLHttpRequest) {
        xmlhttp3 = new XMLHttpRequest();
    } else {
        xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp3.onreadystatechange=function() {
    if (xmlhttp3.readyState==4 && xmlhttp3.status==200) {

    }
}

xmlhttp3.open("GET","includes/trackEnd.php",true);
    xmlhttp3.send();
}

HTML

<script src="../includes/jsHandler.js"></script>
<script>
    window.onload = function(){ return start('<?php echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['PATH_INFO']); ?>');};
    window.onbeforeunload = function(){ return end(); };
    window.onclose = function(){ return end(); };
</script>

谢谢!

1 个答案:

答案 0 :(得分:0)

解决!我正在使用JQuery:

$(window).on('beforeunload', function(){
        return end();
      });
      $(window).on('unload', function(){
        return end();
      });