Created
May 4, 2011 17:45
-
-
Save eduardocereto/955642 to your computer and use it in GitHub Desktop.
a cross-browser implementation of addEventListener/AttachEvent without external dependencies
This file contains hidden or 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
/** | |
* 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; | |
}; |
Im just curious, why are using if else statemants to make a crossbrowser addeventlistener function if everbody else on the internet is saying that this is bad practise. Can you explain a little bit about this descision?
All modern browsers including IE9, supports addEventListener see http://caniuse.com/#feat=addeventlistener
@jcubic I can see only IE11
@aamirafridi you need to expand all to see IE10 and IE9. It's measleading because it never show prev versions of IE
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
ORIGINAL COMMENT: 12-January-2014.
UPDATED 13-January-2014.
I was intrigued with your implementation and I believe I added some best practices to the code in general and also added some improvements to the "traditional" way.
All three implementations of the addEvent custom method below (meaning: with or without any of the addEventListener or attachEvent -- forcing the browser to test all three) worked for:
CHROME: Version 32.0.1700.72 m
FIREFOX: 26.0
EXPLORER: Version 10.0.9200.16750
Needless to say; I didn't examine all possible scenarios in my test cases, only a few...
Let me know what you think.
The first two (with addEventListener or attachEvent) run as the original ones, didn't notice any differences. But for the "traditional way":
My original test was 150k repetitions of adding an empty function to the element's event and then run the event. But as you wrap the handlers onto each other; javascript sends the next error: "Maximum call stack size exceeded" which is only natural.
Then I tested for the maximum stack size allowed which was 7816 ( I made that my test size), the results of adding 7816 empty functions to the same type of event of the same element and then executing the event was:
Your code: minimum = 19ms, maximum = 33ms, average = 30ms.
My code: minimum = 20ms, maximum = 37ms, average = 27ms.
There is obviously not an improvement on performance whatsoever, but we can now delete specific handlers and also we have room for more handlers, and we can use this to standarize our code with the same function to add and to remove events, so we don't have to worry about X-browser considerations.
If we were to have very little to none memory available, I would definitely go with your implementation.
--> Tests done with a Sony vaio 8GB RAM, core i7 second generation.
i.e.