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
    }

     

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

  • 相关阅读:
    hdu 1088 HTML解析
    hdu1171 转化01背包,组合
    Java编程优化之旅(一)一般化方法
    Java简单实现Socket非阻塞通信
    Maven安装,以及导入Intellij IDEA
    笔记本的使用技巧
    Intellij IDEA使用小技巧
    学习Spring有关知识
    学习安装IntelliJ IDEA
    C#后台调用js方法无效果,未解决。
  • 原文地址:https://www.cnblogs.com/moweiran/p/3501266.html
Copyright © 2011-2022 走看看