以下两个代码块均适用于Chrome和Firefox,但不适用于Internet Explorer 11
我搜索了论坛,但我还没有找到任何解决方案:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var starttime = (new Date()).getTime();
$(window).unload(function(){
$.post('timetest.php', {spent: (new Date()).getTime() - starttime});
});
});
</script>
<script>
function start()
{
starttime = (new Date()).getTime();
alert(starttime);
}
function leave()
{
stoptime = (new Date()).getTime();
var params = "spent="+(stoptime-starttime);
xmlobj = new XMLHttpRequest();
xmlobj.open('POST', 'timetest.php', true);
xmlobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlobj.setRequestHeader("Content-length", params.length);
xmlobj.setRequestHeader("Connection", "close");
xmlobj.send(params);
}
</script>
<body onload=start() onunload=leave()>