admin管理员组文章数量:1389903
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
1 Answer
Reset to default 5To 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.
本文标签: javascriptReplace addEventListenerStack Overflow
版权声明:本文标题:javascript - Replace addEventListener - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744601059a2615067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论