简体   繁体   中英

Using JavaScript or jQuery how to check if event exists on the window?

Setup

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)

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

Is this possible?

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.

You can use the in operator...

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

If supported, the property will exist, though the value will be null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM