检测不支持 onunload/onbeforeunload 的浏览器

发布于 2024-12-06 15:54:55 字数 524 浏览 1 评论 0原文

在所有浏览器中,似乎只有 Opera 不支持 onunload/onbeforeunload 事件。 (现在已经十五年了,Opera!)这个问题的解决方案已经被讨论过很多次了,例如:onbeforeunload 支持检测

不幸的是,从 Opera 11.51 开始,("onbeforeunload" in window) == true,但实际的 onbeforeunload 事件从未执行!

当用户离开页面时,我的 Web 应用程序需要将数据发送到服务器;我为此使用同步 ajax 请求。看来我必须求助于使用页面上某处的“保存”按钮来掩盖 Opera 问题。然而,我不希望这个按钮让那些浏览器能够通过 ajax 自动保存的用户感到困惑,所以我真的希望这个按钮只出现在 Opera 中。

我唯一的选择是浏览器检测吗?问题是,Opera 无论如何都可以选择将自己伪装成其他浏览器。

Of all the browsers, it seems that only Opera doesn't support onunload/onbeforeunload events. (It's been fifteen years now, Opera!) Solutions for this issue have been covered many times, here for example: onbeforeunload support detection

Unfortunately, as of Opera 11.51, ("onbeforeunload" in window) == true, but the actual onbeforeunload event is never executed!

My web application needs to send data to server when a user leaves the page; I'm using a synchronous ajax request for this. It looks like I have to resort to using a "Save" button somewhere on the page to cover up for Opera issues. However, I don't want this button to confuse users whose browsers are capable of auto-saving through ajax, so I'd really like the button to only show up in Opera.

Is my only choice browser-detection? The problem is, Opera has an option to disguise itself as other browsers anyway.

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

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

发布评论

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

评论(4

原谅我要高飞 2024-12-13 15:54:55

我无法重现您在 Opera 11.5x 中发现 'onbeforeunload' in windowtrue 的结果。这是最好的方法,应该仍然有效。您确定您没有在某个地方留下某些定义吗?例如,您

onbeforeunload = function (){ ... }

稍后在进行特征检测的同一脚本中编写了这些定义?如果你执行alert(window.onbeforeunload),你会看到什么?您能分享一下有问题的页面的链接吗?

I can't reproduce your finding that 'onbeforeunload' in window is true in Opera 11.5x. This is the best way to do it and should still work. Are you sure you haven't left in some definition somewhere, e.g. you've written

onbeforeunload = function (){ ... }

later in the same script that does the feature detection? If you do alert(window.onbeforeunload), what do you see? Could you share a link to the page with the problem?

凹づ凸ル 2024-12-13 15:54:55

歌剧把这件事搞砸了。我通过查找 window.opera 来检测 Opera,如果它存在,我会拒绝 Opera 无法处理的内容。

我认为使用卸载没有什么好处,因为它在游戏中发生得太晚了。有时 onbeforeunload 是唯一可以解决问题的方法。再次,我只是在窗口对象上查找 opera,如果存在,则拒绝它无法执行的操作。 :)

PPK 在这里讨论它: http://www.quirksmode.org/js/detect.html

Opera screwed the pooch on this one. I detect for Opera by looking for window.opera and if it exists, I deny Opera what it can't handle.

Using unload is no good I think, because it occurs too late in the game. Sometimes onbeforeunload is the only thing that'll do the trick. Once again, I just look for opera on the window object, and, if it exists, deny it the things it can't do. :)

PPK talks about it here: http://www.quirksmode.org/js/detect.html

花开半夏魅人心 2024-12-13 15:54:55

对于任何偶然发现这篇文章的人,这是我用来检测 onbeforeunload 支持的代码片段,如果浏览器不支持它,我会切换到 onunload (注意使用 jquery,显然不是必需的)。就我而言,我使用此代码(以及一些额外的代码)来检查是否有任何 AJAX 请求仍然处于活动状态并阻止用户离开。请记住,使用 onunload 并不理想,因为浏览器仍然会离开页面,但至少它让您有机会警告用户数据可能已丢失,他们应该返回并检查。

您会注意到我正在使用 isEventSupported() 函数,可在 https://github.com/kangax/iseventsupported< /a> 用于跨浏览器支持检测可用事件。

// If the browser supports 'onbeforeunload'
if (isEventSupported('beforeunload', window)) {
  $(window).on('beforeunload', function(){
    return 'This page is still sending or receiving data from our server, if you recently submitted data on this page please wait a little longer before leaving.';
  });
} 
// The browser doesn't support 'onbeforeunload' (Such as Opera), do the next best thing 'onunload'.
else {
  $(window).on('unload', function(){
    alert('This page was still sending or receiving data from our server when you navigated away from it, we would recommend navigating back to the page and ensure your data was submitted.');
  });
}

For anyone stumbling across this post, this is a code snippet I use for detecting onbeforeunload support and if the browser doesn't support it I switch to onunload (note the use of jquery, obviously not required). In my case I use this code (and a little extra) to check if any AJAX requests are still active and stop the user navigating away. Keep in mind that using onunload isn't ideal because the browser will still navigate away from the page but at least it gives you a chance to warn the user that data might have been lost and they should go back and check.

You'll notice I'm using the isEventSupported() function available at https://github.com/kangax/iseventsupported for cross browser support detecting available events.

// If the browser supports 'onbeforeunload'
if (isEventSupported('beforeunload', window)) {
  $(window).on('beforeunload', function(){
    return 'This page is still sending or receiving data from our server, if you recently submitted data on this page please wait a little longer before leaving.';
  });
} 
// The browser doesn't support 'onbeforeunload' (Such as Opera), do the next best thing 'onunload'.
else {
  $(window).on('unload', function(){
    alert('This page was still sending or receiving data from our server when you navigated away from it, we would recommend navigating back to the page and ensure your data was submitted.');
  });
}
喵星人汪星人 2024-12-13 15:54:55

请参阅我的对类似/重复问题的回答。基本上,它会在您域的第一页上设置检测,并将所有后续页面的检测结果存储在 localStorage 中。包括工作示例代码。

See my answer to a similar / duplicated question. Basically, it sets up detection on the very first page on your domain and stores that detection result for all subsequent pages in localStorage. Including working example code.

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