As @rsp's answer correctly solves the problem of decoupling the right handler, in fact it does not answer the namespace problem. To handle this, you need to do a little more code as follows:
function on(elm, evtName, handler) { evtName.split('.').reduce(function(evtPart, evt) { evt = evt ? evt +'.'+ evtPart : evtPart; elm.addEventListener(evt, handler, true); return evt; }, ''); } function off(elm, evtName, handler) { evtName.split('.').reduce(function(evtPart, evt) { evt = evt ? evt +'.'+ evtPart : evtPart; elm.removeEventListener(evt, handler, true); return evt; }, ''); }
So, to summarize: this actually sets up several event listeners - one for each part of your namespace. Unfortunately, this functionality, unfortunately, is not supported, but, as you see, it can be achieved relatively easily. Just be careful that although this script supports a deep namespace (e.g. scroll.parent.child ), it will bind a lot of event listeners (in this case 3) and therefore not practical.
You could make it more perfect, but it's all done.
Tokimon
source share