当前位置:Gxlcms > JavaScript > 关闭ie窗口清除Session的解决方法_javascript技巧

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

时间:2021-07-01 10:21:17 帮助过:8人阅读

代码如下:

//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';
}
}



Handler1.ashx:
代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;

namespace WebApplication1
{
///


/// $codebehindclassname$ 的摘要说明
///

[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,然后需要在会员退出时点退出连接或按纽退出。在会员直接关闭窗体的时候,没有触发涉及到退出的一系列退出。而这些要等到服务器会话过期才会被清除掉。

有幸的是在网络上终于找到可以捕获用户使用Alt+F4、标题栏按右键关闭、双击标题栏、直接按关闭按钮的事件的方法。当然对于最小化到任务栏再进行关闭是不能捕获的。

代码如下: