window.onunload 仅在 Firefox 中关闭选项卡时触发,而不是整个浏览器关闭

发布于 2024-09-26 02:00:19 字数 792 浏览 0 评论 0原文

更新

因此,在阅读了您的两个答案后,我意识到没有可靠的方法来确定浏览器窗口何时关闭。本来我打算在页面关闭时用它来解锁数据库中的一条记录。基本上,当用户加载页面时,它访问的记录会锁定,然后在页面关闭时解锁。关于如何以不同/更好的方式做到这一点有什么建议吗?我正在考虑在页面上发布一条通知,要求他们必须使用关闭按钮而不是 X 来关闭表单,如果他们使用 X 关闭,记录将保持锁定状态,例如 5 分钟,直到他们可以访问它再次。


这段代码非常简单,因为我只是测试一下。基本上,当我在 IE 和 Chrome 中运行此页面时,警报会正常触发。当我在 Firefox 中仅在一个选项卡中运行它并关闭整个浏览器时,警报永远不会触发。但是,如果我有多个选项卡并将页面作为新选项卡打开,然后使用选项卡上的 X 关闭它,则会触发警报。有谁知道 Firefox 在那里做什么?

任何帮助都是值得赞赏的,因为必须打开多个选项卡才能触发 onunload 是非常不可取的。

<html>
<head runat="server">
    <title></title>
    <script type="text/javascript">

        window.onunload = function() {
            alert("unload event detected!");
        }

    </script>
</head>
<body>
</body>
</html>

UPDATE

So after reading both of your answers I realize there is no reliable way to determine when a browser window is closed. Originally I was planning to use this to unlock a record in the database when the page is closed. Basically when the user loads the page the record it's accessing locks and then unlocks when the page is closed. Any suggestions on how to do this differently/better? I was thinking about just posting a notice on the page that they have to use a close button to close the form rather than the X, and if they do close using the X, the record stays locked for say 5 minutes until they can access it again.


This code is very simple because I was just testing it out. Basically when I run this page in IE and Chrome, the alert fires just fine. When I run it in Firefox, in only a single tab, and close the entire browser, the alert never fires. However, if I have multiple tabs and open the page as a new tab and then close it using the X on the tab, the alert fires. Does anyone know what Firefox is doing back there?

Any help is appreciated, because having to have multiple tabs open just to get onunload to fire is pretty undesirable.

<html>
<head runat="server">
    <title></title>
    <script type="text/javascript">

        window.onunload = function() {
            alert("unload event detected!");
        }

    </script>
</head>
<body>
</body>
</html>

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

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

发布评论

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

评论(2

她说她爱他 2024-10-03 02:00:19

您根本不能指望 onunload 处理程序会触发(不仅仅是 Firefox)。 onbeforeunload 处理程序稍微更可靠,但也只是稍微可靠一点。

更新回答您的后续问题:如果您必须使用锁,那么是的,我肯定会使用服务器端超时(您别无选择;例如,如果用户的工作站蓝屏,您希望在某个时刻解锁该记录)。您可能希望在任何时候看到用户的活动时重置超时(您可以在页面打开时使用 ajax ping),并且正如您所说,为用户提供一种显式释放记录的方法。超时时间应该是多长由您决定,但如果您使用 ajax ping 机制,则没有理由需要 5 分钟——您可以执行一分钟的超时和 30 秒的 ajax ping,甚至30 秒的超时和 15 秒的 ping,等等。取决于您想给服务器施加什么样的负载。

You can't count on onunload handlers firing at all (it's not just Firefox). onbeforeunload handlers are slightly more reliable, but only slightly.

Update Responding to your follow-up question: If you have to use a lock, then yes, I would definitely use a server-side timeout (you have no other choice; if the user's workstation blue-screens, for instance, you want that record unlocked at some point). You probably want to reset the timeout any time you see activity from the user (you could use an ajax ping while the page is open) and, as you said, give the user a means of explicitly releasing the record. How long that timeout should be is up to you, but if you used the ajax ping mechanism, no reason it would even need to be five minutes — you could do a one-minute timeout and a 30-second ajax ping, or even a 30-second timeout and a 15-second ping, whatever. Depends on what kind of load you want to put on the server.

∝单色的世界 2024-10-03 02:00:19

单击 X 关闭浏览器后,Firefox 将立即关闭。由于某种原因,它“忘记”调用 onunload 事件。

Firefox closes immediately after you click the X to close the browser. For some reason it 'forgets' to call the onunload event.

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