最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace addEventListener - Stack Overflow

programmeradmin0浏览0评论

I have a lot of code that use addEventListener in minified libraries, files ..., but I need to make work this code in IE8, so replacing addEventListener with addEvent:

 /*
  * Register the specified handler function to handle events of the specified
  * type on the specified target. Ensure that the handler will always be
  * invoked as a method of the target.
  */
 function addEvent(type, handler) {
     if (this.addEventListener)
         this.addEventListener(type, handler, false);
     else {
         this.attachEvent("on" + type,
             function(event) {
                 return handler.call(this, event);
             });
     }
 }

I can "override" window.addEventListener, but how can I override this method in the other objects?

Thanks in advance Regards

I have a lot of code that use addEventListener in minified libraries, files ..., but I need to make work this code in IE8, so replacing addEventListener with addEvent:

 /*
  * Register the specified handler function to handle events of the specified
  * type on the specified target. Ensure that the handler will always be
  * invoked as a method of the target.
  */
 function addEvent(type, handler) {
     if (this.addEventListener)
         this.addEventListener(type, handler, false);
     else {
         this.attachEvent("on" + type,
             function(event) {
                 return handler.call(this, event);
             });
     }
 }

I can "override" window.addEventListener, but how can I override this method in the other objects?

Thanks in advance Regards

Share Improve this question edited Oct 13, 2014 at 20:03 Mr. Polywhirl 48.9k12 gold badges93 silver badges145 bronze badges asked Oct 13, 2014 at 19:56 DiomedesDiomedes 1254 silver badges14 bronze badges 4
  • you can define window.addEventListener in IE to work as the W3 one, so that all code uses the same code, and new browsers use the native mand instead of forking each time. – dandavis Commented Oct 13, 2014 at 19:57
  • window.addEventListener = window.addEventListener || function addEvent(type, handler) { this.attachEvent("on" + type, function (event) { return handler.call(this, event); }); } – dandavis Commented Oct 13, 2014 at 19:59
  • if i use above code only add addEventListener to window? I need to all objects – Diomedes Commented Oct 13, 2014 at 20:17
  • yeah, the above is just for window. you should use Object.defineProperty to do the same type of thing on Element.prototype (only for IE!). – dandavis Commented Oct 13, 2014 at 20:48
Add a ment  | 

1 Answer 1

Reset to default 5

To access it the same way as you would in modern browsers, you'll want to add the method to Window, HTMLDocument, and Element prototypes.

(function(){
    if (!("addEventListener" in window)) {
        function addEventListener(type, handler) {
            var _this = this;              
            this.attachEvent("on" + type, function(){
                handler.call(_this, window.event);  
            });
        }
        Window.prototype.addEventListener = addEventListener;
        HTMLDocument.prototype.addEventListener = addEventListener;
        Element.prototype.addEventListener = addEventListener;
    }
})();

Note: while we've normalized access to the function and addressed the this issue, we haven't normalized the event to w3c standards. So, it will be missing properties like event.target, event.relatedTarget, etc.

Check out Mozilla's addEventListener() shim.

发布评论

评论列表(0)

  1. 暂无评论