如何在用户离开页面之前与服务器同步并清除会话?

发布于 2024-12-09 19:42:58 字数 890 浏览 0 评论 0原文

有没有什么方法可以检测用户何时离开页面,无论是关闭浏览器、在地址栏中输入新的 URL、单击重定向到其他域的链接等?

这样做的主要目的是执行一些活动,例如:

  • 与服务器同步驻留在客户端
  • 清除服务器会话

中的一些数据我尝试使用窗口的 unload 和 beforeunload 事件,并阅读其他问题,例如: 检测用户何时离开网页的最佳方法? // 有什么办法吗知道用户离开 asp.net 页面? 但我没有找到我期望的答案。

这是一个简化的 js 片段,用于理解我正在尝试的内容:

window.onunload = function(){
    if (theConditionThatINeed){
        doThings();
        SyncWithServerAndAbandonSession(url, localObjects);
    }
    else {
        doNothing();
    }
}

我不想在用户离开之前显示任何类型的确认,因此我认为 onbeforeunload 在这里对我没有帮助。

假设答案是“没有办法做这样的事情”,那么完成我想要的同步和会话清除的推荐做法是什么?

我支持的主要浏览器是 IE >= 7

Is there any way to detect when a user leaves a page, no matter if it's by closing the browser, entering a new URL in the address bar, clicking on a link that redirects to other domain, etc. ?

The main purpose of this would be to perform some activities such as:

  • sync with the server some data that resides in the client side
  • clear server session

I was trying with the window's unload and beforeunload events, and reading other questions like:
Best way to detect when a user leaves a web page?
//
Is there any way to know that user leaving a page with asp.net? but I didn't find the answer I would expect.

Here is a simplified js snippet to understand what I was trying:

window.onunload = function(){
    if (theConditionThatINeed){
        doThings();
        SyncWithServerAndAbandonSession(url, localObjects);
    }
    else {
        doNothing();
    }
}

I don't want to display any kind of confirmation before the user leaves, so I think that the onbeforeunload won't help me here.

Supposing that the answer is "there is no way to do such thing", what would be the recommended practice to accomplish the synchronization and session clearing that I want?

The primary browser that I support is IE >= 7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2024-12-16 19:42:58

正如您已经阅读的那样,不可能可靠地检测用户是否离开您的页面。
一般来说,在客户端存储任何未同步的状态并不是一个好习惯。浏览器很容易关闭或崩溃。

您可以通过 javascript 向自己发送 ajax keepalive 消息,以防用户在您的页面上执行任何操作。再说一次,非常不可靠、浪费和黑客。
短暂超时后自动同步。

查看REST式 Web 应用程序。这个概念很有趣,而且从表面上看,不鼓励在服务器上保存状态信息。您也可以将其应用到客户端。

这通常会导致在 URL 中保留状态信息。 URL 告诉服务器它需要知道的任何信息来服务请求,它不需要任何先前活动的内存(会话)。
我尝试只在会话中保留用户标识信息。我也会摆脱这个,但有些工具和库需要会话中的用户。

As you already read, it is not reliably possible to detect whether the user leaves your page.
Generally it is not good practice to store any unsynced state on the client side. Browsers are easily closed or crashed.

You can send yourself ajax keepalive messages via javascript, in case the user does anything on your page. Again, very unreliable, wasteful and hacky.
Auto-Sync after a short timeout.

Take a look at RESTful web applications. The concept is interesting, and, very superficially spoken, discourages keeping state information on the server. You can apply this to the client as well.

This usually results in keeping state information in the URL. The URL tells the server anything it needs to know to service the request, it should not need a memory (the session) of any previous activity.
I try to only keep the user identification info in the session. I would get rid of this too, but some tools and libs need the user in the session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文