Function addevent (Element, type, handler) { // assign each event handler a unique id if (!handler.$ $guid) handler.$ $guid = addevent.guid++; // create a hash table of event types for the element if (!element.events) element.events = {}; // create a hash table of event handlers for each element/event pair var handlers = element.events[type]; if (!handlers) { handlers = element.events[type] = {}; // store the existing event handler (If there is one) if (element["on" + type]) { handlers[0] = element["on" + type]; } } // store the event handler in the hash table handlers[ handler.$ $guid] = handler; // assign a global event handler to do all the work element["on" + type] = handleevent;};/ / a counter used to create unique idsaddevent.guid = 1;function removeevent (Element, type, handler) { // delete the event handler from the hash table if ( element.events&Nbsp;&& element.events[type]) { delete element.events[type][handler.$ $guid]; }};function handleevent (event) { // grab the event object (ie uses a global Event object) event = event | | window.event; // get a reference to the hash table of event handlers var handlers = this.events[ event.type]; // execute each event handler for (var i in handlers) { this.$$ handleevent = handlers[i]; this.$ $handleEvent (event); }};
Characteristics:
1.it performs no object detection
2.it does not use the addeventListener
/ attachEvent
methods
3.it keeps the correct scope (the this
keyword)
4.it passes the event object correctly
5.It is entirely cross-browser (it'll probably work on IE4 and NS4)
6. And from whatI can tell it does not leak memory
Dean Edward the Great God Cross browser addevent event