简体   繁体   中英

is there any way to instrument javascript event handler firing using javascript?

is there any universal way to instrument javascript event handler firing using javascript? for example, I want to do something before the event handler firing, so when to fire an event, I would like to execute my code first then the event handler code.

The problem is that there are multiple ways to register event handlers, I would like to handle all of them: html, javascript

No, there's not.

You can hook addEventListener and removeEventListener , which would allow you to intercept both the registration and invocation of event listeners by JS code. However, this will not capture event listeners set in ways such as elt.onclick . Nor of course will it catch listeners set up via the old IE attachEvent API. Most importantly, it will not help with you that events that are generated and listened for internally, such as a mouse click on a check box.

You might be tempted to hook createEvent and dispatchEvent in similar fashion, but that will capture only events that are explicitly created or dispatched in the JS code.

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