Using JavaScript or jQuery, how to check if an event exists in a window?

Customization

I attached the event to the 'window' object, and I would like to test it there using code.

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

Attempts

I tried simple and jQuery with no luck. I have more attempts on jsFiddle .

 window.beforeunload //is undefined as is window.onbeforeunload $(window).data('events') //not working either 

Is it possible?

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

+4
source share
1 answer

You can use the in ... operator

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

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

+9
source

Source: https://habr.com/ru/post/1396319/


All Articles