javascript - 'attacheventlistener' and 'onclick' prototype -


maybe there serval sub-questions problem:

  • is there general solution events binded element?(without jquery or others javascript librarys).
  • i found $(element).data('events') can't event registered <element onclick="some_functions()">. how can it?

my goal insert event before native event (registered programmer of html page). found 2 solutions.

however, find both these methods can't solve situation

<element onclick="some_functions()"> 

you can access (and overwrite) event handler via onclick property. if have a

<span id="elem" onclick="some_functions()"> 

then document.getelementbyid("elem").onclick yield anonymous function

function (event) {     some_functions() } 

to intercept it, can use like

var fn = elem.onclick; elem.onclick = function() {     console.log("before");     var res = fn.apply(this, arguments);     console.log("after");     return res; }; 

disclaimer: might not in older browsers, don't know how inventors (netscape, ie) of inline attribute event handlers , traditional event registration handled these. might have done eval(elem.getattribute("onclick")), in case can access handler code (as string) that.


Comments