使用JavaScript或jQuery如何检查窗口上是否存在事件? [英] Using JavaScript or jQuery how to check if event exists on the window?

查看:87
本文介绍了使用JavaScript或jQuery如何检查窗口上是否存在事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将一个事件附加到'window'对象,我想通过代码检查它是否存在。

I've attached an event to the 'window' object, and I would like to check that it's there via code.

window.addEventListener('beforeunload', function(e){ /*...*/ }, false)



尝试



我尝试过简单的jQuery,没有运气。我对 jsFiddle 进行了更多尝试。

Attempts

I've tried the simple, and jQuery, without luck. I have more attempts up on jsFiddle.

window.beforeunload //is undefined as is window.onbeforeunload

$(window).data('events') //not working either

这可能吗?

有类似的问题(这里这里)关于DOM和其他元素,但是我尝试过的方法都没有奏效。

There are similar questions (here and here) about the DOM, and other elements, but none of the approaches in those that I have tried have worked.

推荐答案

你可以使用 operator ...

You can use the in operator...

'onbeforeunload' in window;  // true (if supported)






如果支持,该属性将存在,​​但值将为 null

这篇关于使用JavaScript或jQuery如何检查窗口上是否存在事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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