Skip to content

Instantly share code, notes, and snippets.

@pgarciacamou
Last active August 29, 2015 14:15
Show Gist options
  • Save pgarciacamou/49e447ab203794693f05 to your computer and use it in GitHub Desktop.
Save pgarciacamou/49e447ab203794693f05 to your computer and use it in GitHub Desktop.
Adds events in a backwards compatible way (legacy versions of IE).
(function(){
Object.defineProperties(document, {
"on": {
value: function(evName, callback, useCapture){
return addEvent.call(this, evName, callback, useCapture);
}
}
});
Object.defineProperties(window, {
"on": {
value: function(evName, callback, useCapture){
return addEvent.call(this, evName, callback, useCapture);
}
}
});
window.elem = function elem(selector){ return document.querySelectorAll(selector); };
Object.defineProperties(NodeList.prototype, {
"each": {
value: function(fn){
var self = this;
Array.prototype.slice.call(this, 0).forEach(function (){
fn.apply(arguments[0], arguments);
});
return self;
}
},
"on": {
value: function(evName, callback, useCapture){
this.each(function (elem){
addEvent.call(elem, evName, callback, useCapture);
});
return this;
}
}
});
function addEvent(evName, callback, useCapture){
if(this.addEventListener) this.addEventListener(evName, callback, useCapture || false);
else if(this.attachEvent) this.attachEvent("on" + evName, callback);
return this;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment