文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>利用Ajax在web页面关闭时清除session

利用Ajax在web页面关闭时清除session

时间:2010-09-16  来源:暗※空

ASP.net 关闭页面时清空Session
  在登陆的时候想必大家都是用Session保存相关的信息的,Session是保存在服务端的,

当客户端页面关闭时Session仍保存在服务端,直到时间超时才会清掉。那么,有没有办法在
客户端关闭的时候清掉Session呢。 
  要在客户端改变服务端的内容那是不可能,要清掉Session必须回到服务端。不过我们可
以变通下——使用ajax。
  首先我们要判断用户什么时候关闭了页面,这样才能执行下一步动作。不过HTML DOM没
要页面关闭的事件,只有onunload和onbeforeunload是与页面关闭有关的,onunload是页面
关闭或刷新后的事件,onbeforeunload是页面关闭或刷新前的事件,所以我们要用的是
onbeforeunload。
  但是这里就有个问题了,用户有可能是刷新页面而引起的onbeforeunload事件,而我们并
不希望在刷新时清除Session,所以要判断下用户是关闭页面还是在刷新页面。代码如下:

window.onbeforeunload = function()
{  
 //这是网上找的,具体没验证过
      var n = window.event.screenX - window.screenLeft;
      var b = n > document.documentElement.scrollWidth-20;
      if(b && window.event.clientY < 0 || window.event.altKey)  
      {  
          ClearSession();
      }  
}

ClearSession()为ajax调用请求服务端,服务端接收到请求后执行清空Session的操作。Ajax
的东西不多说了,下面为代码。


========================Default.aspx 开始===========================================

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
 
    <script type="text/javascript" src="script.js"></script>
 
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server"></asp:Label>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="写入Session" /></div>
    </form>
</body>
</html>
========================Default.aspx 结束===========================================


========================Default.aspx.cs 开始===========================================

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!string.IsNullOrEmpty(Request.QueryString["___command"]))
        {
            string cmd = Request.QueryString["___command"];
            if (cmd == "ClearSession")
                Session.Remove("name");//清空Session
        }
 
        if (Session["name"] != null)
            this.Label1.Text = Session["name"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["name"] = "vvvvvvvvvvvvv";
        if (Session["name"] != null)
            this.Label1.Text = Session["name"].ToString();
    }
}
 
========================Default.aspx.cs 结束===========================================

 


========================script.js 开始===========================================
function GetXmlHttpObject()
{
    //创建XMLHttpRequest对象来发送和接收HTTP请求与响应
    xmlHttpObj = null;
    try
    {
        // FireFox Opera 8.0+ Safari
        xmlHttpObj = new XMLHttpRequest();
        if(xmlHttpObj.overrideMimeType)
        {
            xmlHttpObj.overrideMimeType('text/xml');
        }
    }
    catch(e)
    {
        // IE
        try
        {
            xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttpObj;
}
 
function StateChanged()
{
    if(___xmlHttp.readyState == 4)
    {
        if(___xmlHttp.status == 200)
        {
        }
        else
        {
        }
    }
}
 
var ___xmlHttp=null;
function ClearSession()
{
    if(___xmlHttp==null)
        ___xmlHttp = GetXmlHttpObject();
    if(___xmlHttp == null)
        return false;
       
    var url = "?___command=ClearSession&___clientRandom=" + Math.random();
 
    ___xmlHttp.open("GET", url, true);
    ___xmlHttp.onreadystatechange = StateChanged;
    ___xmlHttp.send(null);
   
}
 
window.onbeforeunload = function()
{  
      var n = window.event.screenX - window.screenLeft;
      var b = n > document.documentElement.scrollWidth-20;
      if(b && window.event.clientY < 0 || window.event.altKey)  
      {  
          ClearSession();
      }  

========================script.js 结束===========================================


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/meheartfly/archive/2009/09/16/4557673.aspx

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载