浏览器/标签接近结束会话 [英] End Session on browser/tab close

查看:117
本文介绍了浏览器/标签接近结束会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过浏览了互联网。我找不到这样的妥善解决。有没有在浏览器或选项卡不注销关闭一个适当的方式杀人/结束会议?

I have browsed through out the internet. I can't find a proper solution for this. Is there a proper way to kill/end session when browser or tab is closed without logging off?

我已经尝试了所有的codeS在JavaScript中,我发现在互联网上。他们没有工作。

I have tried all the codes in javascript that I found on internet. None of them work.

我所有的用户只使用IE浏览器。因此,code可以为单独工作。任何技术的我没意见,即,JavaScript的,jQuery的,阿贾克斯。对此有任何解决方案?我知道HTTP是无状态的,浏览器事件是客户端和所有。但是,有人应该有一个工作code这一点。请大家帮帮忙。

All my users use IE only. So the code can work for that alone. Any tech is fine with me ie, javascript, jquery, ajax. Is there any solution for this? I know HTTP is stateless, browser event is client side and all that. But someone should have a working code for this. Please help.

推荐答案

当用户关闭浏览器,会话不会字面上届满20分钟的默认时间。当会话过期的OnEnd事件触发。您可以code反对这一点,像这样Global.asax文件...

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

如果你调用Session.Abandon方法OnEnd事件将立即释放了。 (不太不过,因为我注意到,它采取了几分钟,在一定的时间记录显示到数据库中,但是这是我们要获得最好的)。现在,我们需要一些方法来调用Session。 Abandon方法,当用户关闭浏览器。这一招在所有的Web浏览器不会工作,但你可以进军onunload()事件像这样... TESTA.ASPX

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';">
...

当用户关闭在TestA.aspx浏览器窗口,他们将被重定向到TestB.aspx ... 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

在TestB.aspx的页面加载事件中,code放弃会话然后立即关闭浏览器。用户永远不会看到这个页面。其实给用户,它看起来像他们关闭TestA.aspx。

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.

这段JavaScript技术加快杀死会话和呼叫的Session_OnEnd的过程中,但不支持所有的浏览器。对于旧的浏览器,应用程序只是要去要遭受20分钟的会议超时周期。

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

这篇关于浏览器/标签接近结束会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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