Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@monolithed
Created September 28, 2012 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monolithed/3800591 to your computer and use it in GitHub Desktop.
Save monolithed/3800591 to your computer and use it in GitHub Desktop.
DOM Level 2 Events: handleEvent
/*
* DOM Level 2 Events: handleEvent
* @author Alexander Guinnes, Thomas Aylott (original author)
* @description http://ajaxian.com/archives/an-alternative-way-to-addeventlistener
**/
function EventListener(element, phase)
{
var listener = {
addEvent: function (type, callback)
{
var stack = (this["@" + type] || (this["@" + type] = []));
if (!~stack.indexOf(callback))
stack.push(callback);
return this;
},
handleEvent: function (event)
{
var stack = this["@" + event.type];
if (stack)
{
stack.forEach(function (callback, index, stack)
{
if (callback.call(this, event, callback, this) === false) {
this["@" + event.type] = stack.slice();
stack.length = 0;
}
}, this);
}
},
removeEvent: function (type, callback)
{
var stack = this["@" + type], i;
if (stack && ~(i = stack.indexOf(callback)))
stack.splice(i, 1);
return this;
}
};
element.addEventListener('click', listener, phase || false);
return listener;
}
var listen = new EventListener(document);
listen.addEvent("click", function (event) {
alert(event.target);
});
@termi
Copy link

termi commented Sep 28, 2012

Looks so frustrating. Can you provide an example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment