Skip to content

Instantly share code, notes, and snippets.

@eduardocereto
Created May 4, 2011 17:45
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save eduardocereto/955642 to your computer and use it in GitHub Desktop.
Save eduardocereto/955642 to your computer and use it in GitHub Desktop.
a cross-browser implementation of addEventListener/AttachEvent without external dependencies
/**
* Cross Browser helper to addEventListener.
*
* @param {HTMLElement} obj The Element to attach event to.
* @param {string} evt The event that will trigger the binded function.
* @param {function(event)} fnc The function to bind to the element.
* @return {boolean} true if it was successfuly binded.
*/
var cb_addEventListener = function(obj, evt, fnc) {
// W3C model
if (obj.addEventListener) {
obj.addEventListener(evt, fnc, false);
return true;
}
// Microsoft model
else if (obj.attachEvent) {
return obj.attachEvent('on' + evt, fnc);
}
// Browser don't support W3C or MSFT model, go on with traditional
else {
evt = 'on'+evt;
if(typeof obj[evt] === 'function'){
// Object already has a function on traditional
// Let's wrap it with our own function inside another function
fnc = (function(f1,f2){
return function(){
f1.apply(this,arguments);
f2.apply(this,arguments);
}
})(obj[evt], fnc);
}
obj[evt] = fnc;
return true;
}
return false;
};
@aamirafridi
Copy link

@jcubic I can see only IE11
image

@jcubic
Copy link

jcubic commented Jul 10, 2018

@aamirafridi you need to expand all to see IE10 and IE9. It's measleading because it never show prev versions of IE

@luislobo14rap
Copy link

I liked it there, I didn't test it because I don't need it, but reviewing things for old browsers is very interesting

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