Friday, April 15, 2011

Access events added with attachEvent() / addEventListener() in JavaScript

How can I access events added with attachEvent() / addEventListener() in JavaScript?

Use case: debug events using FireBug's console.

From stackoverflow
  • There is no way to access them.

    Depending on what you're trying to achieve, better way to debug the events might be to output the event properties you're interested in from the event handler function...

  • If you always add and remove handlers with a custom method, you can maintain a log of them in the same method. It adds some overhead to do so.

    For example, here is a piece that concerns IE-

    //Run=window.Run || {Shadow:{},nextid:0};
    
    else if(window.attachEvent){    
        Run.handler= function(who, what, fun){
         if(who.attachEvent){
    
          who.attachEvent('on'+what, fun);
    
          var hoo=who.id || who.tagName+(++Run.nextid);
          if(!Run.Shadow[hoo])Run.Shadow[hoo]={};
          if(!Run.Shadow[hoo][what])Run.Shadow[hoo][what]=[];
          Run.Shadow[hoo][what].push(fun);
         }
        }
    }
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.