ページ滞在時間を計算(onLoad, onUnload, getTime)
<html>
<head>
<title>ページ滞在時間を計算</title>
<script language="JavaScript">
<!--
function sta() {
indate = new Date(); //開始時間
intime = indate.getTime();
}
function sto() {
outdate = new Date(); //終了時間
outtime = outdate.getTime();
time = outtime - intime; //滞在時間計算
min = Math.floor(time / 60000); //分
sec = Math.floor((time % 60000) / 1000); //秒
msg = "滞在時間:" + min + "分" + sec + "秒";
alert(msg); //滞在時間表示
}
//-->
</script>
</head>
<body onLoad="sta()" onUnload="sto()">
このページは滞在時間を計測しています。<br>
ページ移動時に表示します。
</body>
</html>
〔 実行する 〕