onbeforeunload支持检测 [英] onbeforeunload support detection

查看:87
本文介绍了onbeforeunload支持检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查当前浏览器是否支持onbeforeunload事件。
常见的javascript方式似乎不起作用:

I'd like to check if the current browser supports the onbeforeunload event. The common javascript way to do this does not seem to work:

if (window.onbeforeunload) {
    alert('yes');
}
else {
    alert('no');
}

实际上,它只会检查某个处理程序是否已附加到事件中。
有没有办法检测是否支持onbeforeunload而不检测特定的浏览器名称?

Actually, it only checks whether some handler has been attached to the event. Is there a way to detect if onbeforeunload is supported without detecting the particular browser name?

推荐答案

我写了一篇关于a在现代浏览器中,或多或少的可靠的推理检测事件支持前一段时间。您可以在演示页面上看到至少在Safari 4 +,FF3.x +和IE中支持beforeunload。

I wrote about a more-or-less reliable inference for detecting event support in modern browsers some time ago. You can see on a demo page that "beforeunload" is supported in at least Safari 4+, FF3.x+ and IE.

编辑:此技术现在用于jQuery,Prototype.js,Modernizr以及其他脚本和库。

Edit: This technique is now used in jQuery, Prototype.js, Modernizr, and likely other scripts and libraries.

这篇关于onbeforeunload支持检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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