想要用户离开当前页面时执行一个事件,我能想到的就是window.onunload和window.onbeforeunload两个事件。比如原来关闭窗口时经常会遇到跳出个广告窗口就是这么做的。
而window.onbeforeunload是在关闭窗口前执行没有保存的就想要离开当前页的话,就会跳出个提示,只有点了确认才能离开当前页面,这就是一个很好的应用。
window.onbeforeunload = function(e){
e = e || window.event;
e.returnValue="真的要走?";
}
如果打算使用Mootools的事件来完成,就要稍微变动一下,因为Mootools对浏览器的事件进行了包装,我们可以通过e.event来得到原来的事件
,前面的这个e就是Mootools包装后的事件:
window.addEvent('beforeunload',function(e){
e.event.returnValue="真的要走?";
})
//=======================================================
window.onbeforeunload = function()
{
if(!confirm("真的要走?"))
{
}
else
{
location.href="exit.aspx";
}
};
//登陆页面
string name = ""; //登陆用户名
ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList;
if (list == null)
{
list = new ArrayList();
}
for (int i = 0; i < list.Count; i++)
{
if (name == (list[i] as string))
{
//已经登录了,提示错误信息
//lblError.Text = "此用户已经登录";
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert(/"此用户已经登
录./");</script>");
return;
}
}
list.Add(name);
Application.Add("GLOBAL_USER_LIST", list);
function myRefresh()
{
var httpRequest = new ActiveXObject("microsoft.xmlhttp");
httpRequest.open("GET", "index.aspx", false);
httpRequest.send(null);
//alert(httpRequest.responseText);
}
setInterval("myRefresh()",30000);
//退出页面方法
Response.Expires = -1;
string strUserId = Session["userName"] as string;
Response.Write(strUserId);
//Response.End();
ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList;
if (strUserId != null && list != null)
{
list.Remove(strUserId);
Application.Add("GLOBAL_USER_LIST", list);
}
Session.Clear();
//Global.asax页面
Session_End 事件中添加
private void Logout()
{
string strUserId = Session["userName"] as string;
Response.Write(strUserId);
//Response.End();
ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList;
if (strUserId != null && list != null)
{
list.Remove(strUserId);
Application.Add("GLOBAL_USER_LIST", list);
}
}
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
Hashtable h = new Hashtable();
Context.Cache.Insert("online", h);
}
//web.config中配置
<sessionState mode="InProc" cookieless="false" regenerateExpiredSessionId="true" timeout="1" />
/// <summary>
/// 防止用户重复登陆,在用户将要身份验证前使用
/// </summary>
/// <param name="name">要验证的用户名字</param>
private void PreventRepeatLogin(string name)
{
Hashtable h = (Hashtable)Cache["online"];
if (h != null)
{
IDictionaryEnumerator e1 = h.GetEnumerator();
bool flag = false;
while (e1.MoveNext())
{
if ((string)((ArrayList)e1.Value)[0] == name)
{
flag = true;
break;
}
}
if (flag)
{
TimeSpan ts = System.DateTime.Now.Subtract(Convert.ToDateTime(((ArrayList)e1.Value)[1]));
if (ts.TotalSeconds < 60)
throw new MyException("对不起,你输入的账户正在被使用中,如果你是这个账户的真正主人,请在下次登陆时及时的更改你的密码,因为你的密码极有可能被盗窃了!");
else
{
h.Remove(e1.Key);
}
}
}
else
{
h = new Hashtable();
}
ArrayList al = new ArrayList();
al.Add(name);
al.Add(System.DateTime.Now);
h[Session.SessionID] = al;
if (Cache["online"] == null)
{
Context.Cache.Insert("online", h);
}
else
Cache["Online"] = h;
}
/// <summary>
/// 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用
/// </summary>
public void LogoutCache()
{
Hashtable h = (Hashtable)Context.Cache["online"];
if (h != null)
{
if (h[Session.SessionID] != null)
h.Remove(Session.SessionID);
Context.Cache["online"] = h;
}
}
/// <summary>
/// 每次刷新本页面的时候更新Cache里的登陆时间选项,在下面的OnInit方法里调用.
/// </summary>
private void UpdateCacheTime()
{
Hashtable h = (Hashtable)Cache["online"];
if (h != null)
{
((ArrayList)h[Session.SessionID])[1] = DateTime.Now;
}
Cache["Online"] = h;
}