zoukankan      html  css  js  c++  java
  • asp.net 记录用户打开和关闭页面的时间

    记录打开页面时间:

    在Page_Load事件里记录一下。

    protected void Page_Load(object sender, EventArgs e){

        if (!Page.IsPostBack)
        {

              //记录用户打开页面时间
               Log_User_Time.SetUserTime(Session, Request, Request.Url.PathAndQuery, DateTime.Now, true);

        }

    }

     

    记录关闭页面的时间:

    通过触发页面的onbeforeunload 事件,JS异步提到处理程序,记录时间。

        window.onbeforeunload = LogUserOutTime;
        var requestObj = null;
        function LogUserOutTime() {
            requestObj = getXMLHttpRequest();
            if (!!requestObj) {          
                requestObj.open("GET", "/LogUserOutTime.ashx", true);
                requestObj.onreadystatechange = reqend;
                requestObj.send(null);
            }
        }

        function reqend() {        
        }

        function getXMLHttpRequest() {
            var xmlRequestObj = null;
            try {
                if (window.ActiveXObject) {
                    // Internet Explorer
                    try {
                        xmlRequestObj = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            xmlRequestObj = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {
                            throw e;
                        }
                    }
                } else if (window.XMLHttpRequest) {
                    // Firefox, Opera 8.0+, Safari
                    xmlRequestObj = new window.XMLHttpRequest();
                }
            } catch (e) {
            }
            return xmlRequestObj;
        }

     

    其它的代码:

    LogUserOutTime.ashx 如下所示:

    <%@ WebHandler Language="C#" class="LogUserOutTime" %>

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.SessionState;
    using nnbh.BaseComm.CommonClass;
    using System.Text;

    /// <summary>
    /// 记录用户操作时间
    /// </summary>
    public class LogUserOutTime : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetNoStore();  //设置为不缓存      
            
            Log_User_Time.SetUserTime(context.Session, context.Request, context.Request.UrlReferrer.PathAndQuery, DateTime.Now, false);

            context.Response.Clear();
            context.Response.End();
        }

        #endregion
    }

     

    此方法在关闭页面和关闭浏览器时都会触发。

  • 相关阅读:
    团队作业 总结
    个人作业 Alpha项目测试
    第二次作业
    交互式多媒体图书平台的设计与实现
    基于VS Code的C++语言的构建调试环境搭建指南
    码农的自我修养之必备技能 学习笔记
    工程化编程实战callback接口学习
    如何测评一个软件工程师的计算机网络知识水平和编程能力
    深入理解TCP协议及其源代码
    Socket与系统调用深度分析
  • 原文地址:https://www.cnblogs.com/moweiran/p/3501266.html
Copyright © 2011-2022 走看看