简体   繁体   中英

How to handle all the events in a webpage

I want to get notified about all the events happening in a webpage.

For each single event I am using something like this:

if(document.addEventListener){
    document.addEventListener('click', function(e) { something(e) } , true);
}else{
    if(document.attachEvent){
        document.attachEvent('onclick', function(e) { something(e) });
    }
}

Is there a simple cross-browser way to get notified about all the events in a webpage instead of listening to each event separately(without using jQuery)?

Write a JavaScript application that searches the tags of an HTML document looking for the event attributes. You can then analyze the events any way you want. The output could be written to the current page or be written to another document using the xmlHttpRequest object.

All the events? I seriously doubt you do considering event-bubbling and how much noise onmousemove is going to produce.

Stick with discretely whitelist binding to what you actually care about.

Try to call the function passed as argument inside another function that calls all the notify operation

function addEvent(ev,fun){
  var hand=function(e){
     alert(ev+" event"); //Or other notify operations
     fun();
  }
  if(document.addEventListener){
      document.addEventListener(ev, hand, true);
  }else{
      if(document.attachEvent){
          document.attachEvent(ev, hand);
      }
  }
}

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