JavaScript 清空Session

      众所周知,Session是运行在服务器端的,JavaScript是运行在客户端的,JavaScript不能直接运行服务器端的代码。但最近笔者却遇到了这样的需求:在一个学习系统里面,用户不能同时打开两个在线考试或在线学习的窗口。通过打开模态对话框,的确可以禁止用户再打开一个新窗口,但如果用户重新打开一个新的页面,却可以打开一个新的对话框。

      于是便想到了使用Cookies和Session记录用户打开考试或学习窗口,因为Cookies和Session都有全局作用。但Cookies有个缺点就是,如果用户清空了Cookies,则Cookies值便会丢失,于是便剩下使用Session了。但有个比较棘手的问题是,如果用户直接按浏览器的关闭按钮退出窗口怎么办?因为这样根本就不会执行后台的代码。于是便很容易想到了使用Ajax请求的方式。如下面代码:

      在在线学习页面的cs文件中判断是否已经打开学习页面

1 if (Session["study"]==null || Session["study"]=="")
2 {
3 Session["study"] = "true";
4 }
5  else
6 {
7 string myScript = @"alert('你已经打开一个在线学习窗口,不能同时打开多个在线学习窗口!');
type = 1; window.close(); ";
8 Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", myScript, true);
9 }

      注意,这里注册了一段JavaScript,并且调用了window.close()方法,这种情况下关闭页面是不能清空Session[“study”]的值的。所以加了一个type=1,如果是type==1,则不清空Session值。而前台也声明了一个type=0,当type==0表示是用户直接关闭窗口,是需要清空Session值。

     

1 function window.onbeforeunload() {
2 if (type == 0) {
3 var xmlHttp = createXMLHttpRequest();
4 xmlHttp.open('GET', "EndStudy.aspx", true);
5 xmlHttp.send(null);
6 xmlHttp.onreadystatechange = function() {
7 if (4 == xmlHttp.readyState) {
8 if (200 == xmlHttp.status) {
9 }
10 else {
11 }
12 }
13 }
14 }
15 }

       在EndStudy.aspx.cs中清除Session的值

1 protected void Page_Load(object sender, EventArgs e)
2 {
3 Session["study"] = "";
4 }

      貌似这种方法没有什么问题。如果在关闭窗口前,请求页面清除了Session的值,的确可以实现需求。但我却为此郁闷了很久,发现第一次关闭页面清除Sesson值成功,第二次关闭页面时,请求EndStudy.aspx成功,去没有执行EndStudy.aspx.cs中的Page_Load函数。所以想再次打开在线学习页面,却提示不能“同时打开两个在线学习窗口”。

      后来想到对话框是有缓存的,于是便在在线学习页面和它的承载页面的Page_Load执行了清除Session的代码:

1 Response.Buffer = true;
2 Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
3 Response.Expires = 0;
4 Response.CacheControl = "no-cache";
5 Response.AddHeader("Pragma", "No-Cache");

       但问题依旧,最后在EndStudy.aspx.cs的Page_Load也加了清除Session的代码才解决问题。

       EndStudy.aspx.cs中的Page_Load最终代码如下:

1 protected void Page_Load(object sender, EventArgs e)
2 {
3 Response.Buffer = true;
4 Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
5 Response.Expires = 0;
6 Response.CacheControl = "no-cache";
7 Response.AddHeader("Pragma", "No-Cache");
8
9 Session["study"] = "";
10 }
原文地址:https://www.cnblogs.com/BensonHe/p/1969456.html