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
    }

     

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

  • 相关阅读:
    常用快捷键知识汇总
    按照给定区间产生指定数目的随机数—C#
    OC (3) 类 与 类方法 (便利构造器) iOS
    OC (2) 基础知识分析对象,创建类,自定义初始化方法 iOS
    OC (1) 类和对象:ObjectiveC概述、面向对象编程、类和对象、实例变量操作 iOS
    OC (6) Block、数组高级:Block语法、Block使用、Block实现数组排序 iOS
    OC (7) 类的扩展 iOS
    OC (5) 字典、集、数组排序:字典类、集合类、数组数组排序、字典、集合的快速遍历、数组排序 iOS
    OC (4)NSString、NSArray、NSNumber、使用苹果帮助文档、值对象 iOS
    jquery 里 $(this)的用法
  • 原文地址:https://www.cnblogs.com/moweiran/p/3501266.html
Copyright © 2011-2022 走看看