JavaScriptはページの滞在時間を記録します.

6075 ワード

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>  </title>
<!--zhaoxiaoyang.cnblogs.com/-->
</head>
<body onUnload=stay()><!--//       ,  stay()  。-->
<mce:script language="Javascript"><!--
pageOpen = new Date();//          ,          。
function stay() {//      。
pageClose = new Date();//          ,          。
minutes = (pageClose.getMinutes() - pageOpen.getMinutes()); //“  ”                      。
seconds = (pageClose.getSeconds() - pageOpen.getSeconds());//“  ”                      。
time = (seconds + (minutes * 60));//      “  ”    “  ”    60,       。
time = (time + "   ");
alert('       ' + time + '.      !');
} 
/*alert() JavaScript       ,          OK      ()      ,           。*/
// --></mce:script>
</body>
</html>  
実例
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 
   <mce:script src="js/JScript22.js" mce_src="js/JScript22.js" type="text/javascript"></mce:script> 
    <title></title>
</head>
<body onUnload="stay()">
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
  
pageOpen = new Date(); 


var req11="";

 var url11="InsertTime.aspx"; //         
            if(window.XMLHttpRequest) // IE    IE7(7.0     ), xmlhttprequest    
            {
                req11=new XMLHttpRequest();
            }
            else if(window.ActiveXObject) //IE(6.0     )    activexobject    ,          ActiveX,     .
            {
                req11=new ActiveXObject("Microsoft.XMLHttp");
            }
            
            if(req11) //    xmlhttprequest
            {
                req11.open("GET",url11,true); //        (    post get,  ,true    )
                req11.onreadystatechange = callback1; //      
                req11.send(null); //    
            }
  
        
        function callback1() //    ,         ,  response  
        {
            if(req11.readystate==4) //     4    
            {
                if(req11.status==200) //http  200  OK
                {
                    Dispaly(); //      ,     ,    
                }
                else //http      
                {
                    alert("       " + req11.statusText);
                }
            }
            else //         ,    
            {
              //  document .getElementById ("myTime").innerHTML ="     ";
            }
        }





function stay() { 
pageClose = new Date(); 
minutes = (pageClose.getMinutes() - pageOpen.getMinutes()); 
seconds = (pageClose.getSeconds() - pageOpen.getSeconds()); 
time = (seconds + (minutes * 60)); 
time = (time + "   ");
alert('       ' + time + '.      !');



var req="";

 var url="OutTime.aspx"; //         
            if(window.XMLHttpRequest) // IE    IE7(7.0     ), xmlhttprequest    
            {
                req=new XMLHttpRequest();
            }
            else if(window.ActiveXObject) //IE(6.0     )    activexobject    ,          ActiveX,     .
            {
                req=new ActiveXObject("Microsoft.XMLHttp");
            }
            
            if(req) //    xmlhttprequest
            {
                req.open("GET",url,true); //        (    post get,  ,true    )
                req.onreadystatechange = callback; //      
                req.send(null); //    
            }
  
        
        function callback() //    ,         ,  response  
        {
            if(req.readystate==4) //     4    
            {
                if(req.status==200) //http  200  OK
                {
                    Dispaly(); //      ,     ,    
                }
                else //http      
                {
                    alert("       " + req.statusText);
                }
            }
            else //         ,    
            {
              //  document .getElementById ("myTime").innerHTML ="     ";
            }
        }

} 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class InsertTime : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SysCommon.DbSql.ExecuteSql("insert into usertable (username) values ('1')");
        }
    }
}
  
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class OutTime : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SysCommon.DbSql.ExecuteSql("insert into usertable (username) values ('2')");
        }
    }
}