首页 > 语言 > JavaScript > 正文

关闭ie窗口清除Session的解决方法

2024-05-06 14:30:52
字体:
来源:转载
供稿:网友

代码如下:
//function window.onunload() { alert('这就是你要做的事,关闭网页之后做的! '); location = 'SessionClear.aspx'; }
        //function window.onbeforeunload() { alert('这就是你要做的事,关闭网页之前做的! ') }

        function window.onunload() {

            if ((window.screenLeft >= 10000 && window.screenTop >= 10000) || event.altKey) {
                alert(window.screenLeft+","+window.screenTop);
                //用户非正常关闭时需要触发的动作
                location = 'Handler1.ashx';
            }
        }

    </script>

Handler1.ashx:
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;

namespace WebApplication1
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler1 : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Session.Abandon();
            context.Session.Clear();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

解释:

一般的会员形式的网站,在会员登陆后都会建立会话或者Cookie,然后需要在会员退出时点退出连接或按纽退出。在会员直接关闭窗体的时候,没有触发涉及到退出的一系列退出。而这些要等到服务器会话过期才会被清除掉。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选