<html>
<head>
<title>Latency Tracking Demo</title>
</head>
<body>
<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>
<script type="text/javascript" src="http://www.example.com/scripts/time-tracker.js"></script>
<script type="text/javascript">
var timeTracker = new TimeTracker();
var pageTracker = _gat._getTracker('UA-1735986-1');
</script>
<input type="button" value="Start Timer" onclick="timeTracker._recordStartTime();"/>
<input type="button" value="Stop Timer" onclick="timeTracker._recordEndTime();"/>
<input type="button" value="Track!" onclick="timeTracker._track(pageTracker, undefined, 'Manual Test');"/>
</body>
</html>
我正在尝试将此概念应用于onload JavaScript事件作为开始时间和onunload作为停止时间,如
onload="timeTracker._recordStartTime();"
onunload="timeTracker._recordEndTime(); timeTracker._track(pageTracker, 'Category','Action');"
但是没有得到它的工作..甚至没有抛出错误
任何想法
答案 0 :(得分:1)
function loadIt(){
timeTracker._recordStartTime();
}
function unLoadIt(){
timeTracker._recordEndTime();
timeTracker._track(pageTracker, 'Category','Action');
}
然后(我使用这个功能)
function addOnloadEvent(func){
var oldOnload = window.onload;
if(typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if(oldOnload) {
oldOnload();
}
func();
}
}
}
addOnloadEvent(loadIt);
//window.onunload = unLoadIt //Do the same for unload
它采用函数名称,不带()或引号
不是将其直接分配给onload属性,而是将其添加为事件侦听器
选中此link