Created
January 18, 2012 21:39
-
-
Save JeffreyWay/1635889 to your computer and use it in GitHub Desktop.
Legacy JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// THE GOAL | |
// Write $('ul').on('click', 'a', fn); in JavaScript | |
// Must support old IE (so no Selectors API (matchesSelector) or anything) | |
// Can you shorten this? | |
var addEvent = (function () { | |
if (window.addEventListener) { | |
return function (el, ev, fn) { | |
el.addEventListener(ev, fn, false); | |
}; | |
} else { | |
return function (el, ev, fn) { | |
el.attachEvent('on' + ev, function() { | |
return fn.call(el, window.event); | |
}); | |
}; | |
} | |
}()); | |
var uls = document.getElementsByTagName('ul'); | |
for ( var i = 0, len = uls.length; i < len; i++ ) { | |
addEvent(uls[i], 'click', function(e) { | |
var target = e.target || e.srcElement; | |
if ( target && target.nodeName.toLowerCase() === 'a' ) { | |
// proceed | |
} | |
}); | |
} |
You are correct. My fork operates differently, so I didn't take that into consideration. Also the variable I commented before is the wrong one. :)
@JeffreyWay longer, but worth it: https://gist.github.com/1636094
@jeremy - Oh yeah, forgot about that.
Tweet sized addEvent function:
a=(function(a,b)function(c,d,e)a[b]?c[b].call(c,d,e):c.attachEvent("on"+d,function()e.call(c,a.event)))(window,"addEventListener")
Note that JavaScript 1.8 is required for the function shorthand. Here it is unminified
var addEvent = (function (w,ael) {
return function (el, ev, fn) {
w[ael] ? el[ael].call(el, ev, fn) :
el.attachEvent('on' + ev, function() {
return fn.call(el, w.event);
});
};
}(window,"addEventListener"));
This might also work fine:
1 var addEvent = addEV; 2 function addEV(){ 3 return function (el, ev, fn) { 4 if (window.addEventListener) 5 el.addEventListener(ev, fn, false); 6 else{ 7 el.attachEvent('on' + ev, function() { 8 return fn.call(el, window.event); 9 }); 10 } 11 }; 12 } 13 var uls = document.getElementsByTagName('ul'); 14 15 for ( var i = 0, len = uls.length; i < len; i++ ) { 16 addEvent(uls[i], 'click', function(e) { 17 if ( e.target && e.target.nodeName.toLowerCase() === 'a' ) { 18 // proceed 19 } 20 }); 21 } 22
Or, if we need multiple element event binding...
var addEvent = (function () {
var filter = function(el, type, fn) {
for ( var i = 0, len = el.length; i < len; i++ ) {
addEvent(el[i], type, fn);
}
};
if ( document.addEventListener ) {
return function (el, type, fn) {
if ( el && el.nodeName || el === window ) {
el.addEventListener(type, fn, false);
} else if (el && el.length) {
filter(el, type, fn);
}
};
}
return function (el, type, fn) {
if ( el && el.nodeName || el === window ) {
el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
} else if ( el && el.length ) {
filter(el, type, fn);
}
};
})();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@eliperelman Not by default, but the addEvent() implementation above takes care of that.