要完成此效果需要兩個步驟
第一步:把如下代碼加入到<body>區(qū)域中
<script LANGUAGE="Javascript">
<!-- Begin
var onHours = " ";
var onMinutes = " ";
var onSeconds = " ";
var offHours = 0;
var offMinutes = 0;
var offSeconds = 0;
var logSeconds = 0;
var logMinutes = 0;
var logHours = 0;
var OnTimeValue = " ";
var OffTimeValue = " ";
var PageTimeValue = " ";
function getLogonTime() {
var now = new Date();
var ampm = (now.getHours() >= 12) ? " P.M." : " A.M."
var Hours = now.getHours();
Hours = ((Hours > 12) ? Hours - 12 : Hours);
var Minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();
var Seconds = ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();
OnTimeValue =(" "
+ Hours
+ Minutes
+ Seconds
+ " "
+ ampm);
onHours = now.getHours();
onMinutes = now.getMinutes();
onSeconds = now.getSeconds();
}
function getLogoffTime() {
var now = new Date();
var ampm = (now.getHours() >= 12) ? " P.M." : " A.M."
var Hours = now.getHours();
Hours = ((Hours > 12) ? Hours - 12 : Hours);
var Minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();
var Seconds = ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();
OffTimeValue =(" "
+ Hours
+ Minutes
+ Seconds
+ " "
+ ampm);
offHours = now.getHours();
offMinutes = now.getMinutes();
offSeconds = now.getSeconds();
timer();
}
function timer() {
if (offSeconds >= onSeconds) {
logSeconds = offSeconds - onSeconds; }
else {
offMinutes -= 1;
logSeconds = (offSeconds + 60) - onSeconds;
}
if (offMinutes >= onMinutes) {
logMinutes = offMinutes - onMinutes; }
else {
offHours -= 1;
logMinutes = (offMinutes + 60) - onMinutes;
}
logHours = offHours - onHours;
logHours = ((logHours < 10) ? "0" : ":") + logHours;
logMinutes = ((logMinutes < 10) ? ":0" : ":") + logMinutes;
logSeconds = ((logSeconds < 10) ? ":0" : ":") +logSeconds;
PageTimeValue =(" "
+ logHours
+ logMinutes
+ logSeconds);
displayTimes();
}
function displayTimes() {
alert("“n登陸時間 : " +OnTimeValue+"“n“n現(xiàn)在時間 : "+OffTimeValue+"“n“n停留時間 : " + PageTimeValue);
}
// End -->
</script>
<FORM>
<INPUT TYPE="button" value="看登陸本頁的情況" onClick="getLogoffTime()">
</FORM>
第二步:把“onLoad="getLogonTime()"”加在<body>標記里
例如:<body onLoad="getLogonTime()">
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |