杀死浏览器关闭事件的会话 [英] Kill session on browser close event

查看:100
本文介绍了杀死浏览器关闭事件的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户关闭浏览器后清除我的会话但是我无法在javascript中获取任何事件链接onunload或onbeforeunload。

我也尝试删除Session_End中的会话值在Global.ascx文件中也没有触发。

有没有办法在用户关闭浏览器或当前标签时捕获事件?

解决方案

没有完全证明的技术,但这里有一些可能的解决方案...



当用户关闭浏览器时,会话不会真正到期默认时间为20分钟。当会话到期时,OnEnd事件将触发。您可以在global.asax文件中对此进行编码,如此...



  Sub  Session_OnEnd()
Dim conn As SqlConnection(CONNECT_STRING)
Dim cmd 作为 SqlCommand(_
插入_DummyTbl(文本)值('GLOBAL'),conn)

conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
< span class =code-keyword>结束 Sub







现在,如果您调用Session.Abandon方法,OnEnd事件将立即触发。 (尽管如此,因为我注意到在某些时候需要花费几分钟才能将记录显示在数据库中,但这是我们最好的。)现在我们需要一些方法来调用Session。用户关闭浏览器时的放弃方法。这个技巧在所有Web浏览器中都不起作用,但你可以像这样点击onunload()事件...... TESTA.ASPX

< body onunload =window.location.href ='TestB。 aspx';>

...





当用户关闭TestA的浏览器窗口时.aspx,它们被重定向到TestB.aspx ... TESTB.ASPX

 受保护的 覆盖  Sub  OnLoad( ByVal  e  As  System.EventArgs)
调用 MyBase .OnLoad (e)

' 结束会话
Session.Abandon ()

' 构建一个将关闭此Web浏览器窗口的JavaScript字符串
Dim s As String =
s& = < script language =javascript>
s& = window.close();
s& = < / script>

' 将JavaScript添加到HTML流
Page.RegisterClientScriptBlock( close,s)
结束 Sub











在TestB.aspx的页面加载事件中,代码放弃会话,然后立即关闭浏览器。用户永远不会看到此页面。实际上对于用户来说,看起来他们关闭了TestA.aspx。



这种javascript技术加速了杀死会话和调用Session_OnEnd的过程,但是不支持在所有浏览器上。对于较旧的浏览器,应用程序将不得不忍受20分钟的会话超时时间。



我希望以上信息对您有所帮助。如果您有任何问题或疑虑,请告诉我。我很高兴能得到帮助


http://stackoverflow.com/questions/1921941/close-kill-the-session-when-the-browser-or-tab-is-closed [ ^

I want to clear my session once user close the browser however I am not able to get any events link onunload or onbeforeunload in javascript.
I have also tried to remove the session values in Session_End even in Global.ascx file which is also not firing .
Is there a way to catch the event when user close the browser or current tab?

解决方案

There is no full-proof technique, but here is some probable solutions ...

When a user closes the browser, the session doesn't literally expire for a default time of 20 minutes. When the session expires the OnEnd event fires. You can code against this in the global.asax file like so ...

Sub Session_OnEnd()
    Dim conn As New SqlConnection(CONNECT_STRING)
    Dim cmd As New SqlCommand( _
        "insert into _DummyTbl (Text) values ('GLOBAL')", conn)

    conn.Open()
    cmd.ExecuteNonQuery()
    conn.Close()
End Sub




Now if you call the Session.Abandon method the OnEnd event will fire immediately. (NOT quite though, because I noticed that it took up to a few minutes at certain times for a record to appear into the database, but this is the best we are going to get.) Now we need some way to call the Session.Abandon method when a user closes their browser. This trick wont work in all web browsers, but you can tap into the onunload() event like so ... TESTA.ASPX
<body onunload="window.location.href='TestB.aspx';">
...


When the user closes the browser window at TestA.aspx, they are redirected to TestB.aspx ... TESTB.ASPX

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
   Call MyBase.OnLoad(e)

   ' End The Session
   Session.Abandon()

   ' Build A JavaScript String That Will Close This Web Browser Window
   Dim s As String = ""
   s &= "<script language=""javascript"">"
   s &= "window.close();"
   s &= "</script>"
   
   ' Add The JavaScript To The HTML Stream
   Page.RegisterClientScriptBlock("close", s)
End Sub






On page load event of TestB.aspx, the code abandons the session then closes the browser immediately. The user never sees this page. In fact to the user, it looks like they closed TestA.aspx.

This javascript technique speeds up the process of killing the session and calling Session_OnEnd, but is not supported on all browsers. For older browsers, the application is just gonna have to suffer the 20 minute session timeout period.

I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance


http://stackoverflow.com/questions/1921941/close-kill-the-session-when-the-browser-or-tab-is-closed[^]


这篇关于杀死浏览器关闭事件的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆