我有以下代码来调用onLoad和onBeforeUnload事件:
<script type="text/javascript">
var startime = (new Date()).getTime();
window.onload = record_visit_ol; //ol - onload
window.onbeforeunload = record_visit_obul; //obul = onbeforeunload
function record_visit_ol() {
var x = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
x.open("GET", "count_visit.php?t=" + (((new Date()).getTime() - startime) / 1000)+"&type=ol&url="+escape(window.location.href), false);
x.send(null);
}
function record_visit_obul() {
var x = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
x.open("GET", "count_visit.php?t=" + (((new Date()).getTime() - startime) / 1000)+"&type=obul&url="+escape(window.location.href), false);
x.send(null);
}
这很有效。但是,我有2个事件的2个功能。我曾尝试仅创建1个函数,但它会在onLoad期间触发这两个事件。
这是在加载页面时甚至触发onBeforeUnload事件的代码:
<script type="text/javascript">
var startime = (new Date()).getTime();
window.onload = record_visit('ol'); //ol - onload
window.onbeforeunload = record_visit('obul'); //obul = onbeforeunload
function record_visit(value) {
var x = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
x.open("GET", "count_visit.php?t=" + (((new Date()).getTime() - startime) / 1000)+"&type="+value+"&url="+escape(window.location.href), false);
x.send(null);
}
答案 0 :(得分:3)
问题是你在第二组代码中调用该函数。你需要传递一个函数:
window.onload = function(){
record_visit('ol'); //ol - onload
}
window.onbeforeunload = function(){
record_visit('obul'); //obul = onbeforeunload
}
答案 1 :(得分:0)
试试这个:
window.onload = function(){ record_visit('ol'); }
window.onbeforeunload = function(){ record_visit('obul'); }