在Onload事件里面获得当前时间,在onunload获得离开时间,减一下啦

解决方案 »

  1.   

    试一试我的代码吧
    <HTML>
    <HEAD>
    <TITLE>停留时间</TITLE>
    </HEAD>
    <SCRIPT LANGUAGE="JAVASCRIPT">var ap_name = navigator.appName;
    var ap_vinfo = navigator.appVersion;
    var ap_ver = parseFloat(ap_vinfo.substring(0,ap_vinfo.indexOf('(')));
    var time_start = new Date();
    var clock_start = time_start.getTime();
    var dl_ok=false;
    function init (){
    if(ap_name=="Netscape" && ap_ver>=3.0)
    dl_ok=true; 
    return true;}
    function get_time_spent (){ 
    var time_now = new Date();
    return((time_now.getTime() - clock_start)/1000); }
    function show_secs() // show the time user spent on the side

    var i_total_secs = Math.round(get_time_spent()); 
    var i_secs_spent = i_total_secs % 60;
    var i_mins_spent = Math.round((i_total_secs-30)/60); 
    var s_secs_spent = "" + ((i_secs_spent>9) ? i_secs_spent : "0" + i_secs_spent);
    var s_mins_spent = "" + ((i_mins_spent>9) ? i_mins_spent : "0" + i_mins_spent);
    document.fm0.time_spent.value = s_mins_spent + ":" + s_secs_spent;
    window.setTimeout('show_secs()',1000); }</SCRIPT>
    <BODY onLoad="init(); window.setTimeout('show_secs()',10);">
    <form name="fm0" onSubmit="0">
    <font size="-1"><I><FONT COLOR="#888888">停留时间:</FONT></I></font><br>
    <INPUT type="text" name="time_spent" size=7 onFocus="this.blur()">
    </form>
    </BODY>
    </HTML>
      

  2.   

    原理一样,稍加修改即可,不知是否是你想要的。
    <HTML>
    <HEAD>
    <TITLE>打开时间</TITLE>
    <script language="javascript">
    function makearray(n) {
    this.length = n;
    for(var i = 1; i <= n; i++) 
    this[i] = 0;
    return this;}
    hexa = new makearray(16);
    for(var i = 0; i < 10; i++) 
    hexa[i] = i; 
    hexa[10]="a"; 
    hexa[11]="b"; 
    hexa[12]="c";
    hexa[13]="d"; 
    hexa[14]="e"; 
    hexa[15]="f";
    function hex(i) {
    if (i < 0) 
    return "00";
    else if (i > 255) 
    return "ff"; 
    else return "" + hexa[Math.floor(i/16)] + hexa[i%16];}
    function setbgColor(r, g, b) { 
    var hr = hex(r); 
    var hg = hex(g); 
    var hb = hex(b); 
    document.bgColor = "#"+hr+hg+hb;
    }function fade(sr, sg, sb, er, eg, eb, step) { 
    for(var i = 0; i <= step; i++) { 
    setbgColor( Math.floor(sr * ((step-i)/step) + er * (i/step)), 
    Math.floor(sg * ((step-i)/step) + eg * (i/step)), Math.floor(sb * 
    ((step-i)/step) + eb * (i/step))); }}function fadein() {
    fade(255,0,0,0,0,255,100);
    fade(0,0,255,0,255,0,100);
    fade(0,255,0, 0,0,0, 100);}
    </script>
    </HEAD>
    <SCRIPT LANGUAGE="JAVASCRIPT">var ap_name = navigator.appName;
    var ap_vinfo = navigator.appVersion;
    var ap_ver = parseFloat(ap_vinfo.substring(0,ap_vinfo.indexOf('(')));
    var time_start = new Date();
    var clock_start = time_start.getTime();
    var dl_ok=false;
    function init (){
    if(ap_name=="Netscape" && ap_ver>=3.0)
    dl_ok=true; 
    return true;}
    function get_time_spent (){ 
    var time_now = new Date();
    return((time_now.getTime() - clock_start)/1000); }
    function show_secs() // show the time user spent on the side

    var i_total_secs = Math.round(get_time_spent()); 
    var i_secs_spent = i_total_secs % 60;
    var i_mins_spent = Math.round((i_total_secs-30)/60); 
    var s_secs_spent = "" + ((i_secs_spent>9) ? i_secs_spent : "0" + i_secs_spent);
    var s_mins_spent = "" + ((i_mins_spent>9) ? i_mins_spent : "0" + i_mins_spent);
    document.fm0.time_spent.value = s_mins_spent + ":" + s_secs_spent;
    }
    //window.setTimeout('show_secs()',1000); 
    </SCRIPT>
    <BODY onLoad="init();">
    <form name="fm0" onSubmit="0">
    <font size="-1"><I><FONT COLOR="#888888">打开时间:</FONT></I></font><br>
    <INPUT type="text" name="time_spent" size=7 onFocus="this.blur()">
    </form>
    <script language="javascript">
    fadein();
    show_secs();
    </script>
    </BODY>
    </HTML>
      

  3.   

    <html>
    <head>
    <script language=javascript>var t1 = new Date().getTime();</script>
    </head><body onload=onload='window.status = "打开该网页一共花出 "+ (new Date().getTime()-t1)/1000 +" 秒钟!"'></body>
    </html>