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)
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.
To check whether dynamically attached event listener exists or not with JavaScript, we can get and set the element's listener attribute. export const attachEvent = ( element: Element, eventName: string, callback: () => void ) => { if (element && eventName && element. getAttribute("listener") !== "true") { element.
To check if an element has event listener on it with JavaScript, we can call the getEventListeners function in the Chrome developer console. getEventListeners(document. querySelector("your-element-selector")); in the Chrome developer console to select the element we want to check with querySelector .
jQuery live() Method The live() method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur. Event handlers attached using the live() method will work for both current and FUTURE elements matching the selector (like a new element created by a script).
You can use the in
operator...
'onbeforeunload' in window; // true (if supported)
If supported, the property will exist, though the value will be null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With