Miguel
Miguel

Reputation: 1964

How can I extend event handling safely, and protectedly?

I am writing a small JavaScript library, and in it, I have components that need to receive "messages" when certain page events go off, and allow users to supply response functions. Basically, I need to access the general events, such as onkeydown, and the user can supply how a component will handle it.

Setting a callback like this is easy. The problem I face though, is when I consider the possibility of other libraries and user code working at the same time. I have to not only preserve their existing handling, but make it flexible afterwards too, since someone could just overwrite what I've done.

Is there any patterns or suggestions that could be used here so all code could work in union? I hope I'm making sense with this one. I can provide more details, if needed.

Upvotes: 1

Views: 808

Answers (1)

Eevee
Eevee

Reputation: 48546

Use addEventListener (or, in IE before 9, attachEvent). It's more flexible and doesn't interfere with the on* properties at all.

Upvotes: 3

Related Questions