2

Furthering the answer to this question Failure to override Element's addEventListener in Firefox

I don't want to use external wrapper/utility function mentioned in here. Javascript add events cross-browser function implementation: use attachEvent/addEventListener vs inline events

Objective: I simply call the overridden addEventListener function to keep things simple and it should handle the cross browser differences within itself or please advise.

I've edited the code, how can we achieve the cross browser functionality within this solution

 (function() {
        var interfaces = [ HTMLDivElement, HTMLImageElement /* ... (add as many as needed) */ ];
        for (var i = 0; i < interfaces.length; i++) {
            (function(original) {
                interfaces[i].prototype.addEventListener = function(type, listener, useCapture) {

                    // DO SOMETHING HERE
                    if(element.attachEvent){ //IE
                        arguments[0] = 'on' + arguments[0];
                        return element.attachEvent.apply(this, arguments);
                    } else { //other browsers
                        return original.apply(this, arguments);
                    }
                }
            })(interfaces[i].prototype.addEventListener);
        }
    })();
Community
  • 1
  • 1
  • 2
    Not an answer, but you shouldn't do this. See why at http://stackoverflow.com/questions/3826599/general-reasons-not-to-deal-with-documents-and-elements-prototype – Dr.Molle May 03 '12 at 10:44
  • Stack Overflow isn't a coding service. If you have a *question*, feel free to ask that question. This is not a question. http://stackoverflow.com/faq – T.J. Crowder May 03 '12 at 10:44
  • Separately, note that you **cannot** extend the prototypes of DOM elements on IE. You can extend *individual elements*, but it's amazingly easy to have a code path where you forget to extend one and thus get an error when you try to use `addEventListener` on it. (You see this with the Prototype library all the time.) So with all due respect, you need to just deal with using a wrapper. – T.J. Crowder May 03 '12 at 10:46
  • thanks for the comments, after looking at Dr. Molle's link, this makes sense not to do it and instead take the wrapper approach. –  May 03 '12 at 11:36

0 Answers0