What is the equivalent to the Element Object in Internet Explorer 9?
if (!Element.prototype.addEventListener) { Element.prototype.addEventListener = function() { .. } }
How does it works in Internet Explorer?
If there's a function equal to addEventListener
and I don't know, explain please.
Any help would be appreciated. Feel free to suggest a completely different way of solving the problem.
addEventListener is the proper DOM method to use for attaching event handlers. Internet Explorer (up to version 8) used an alternate attachEvent method. Internet Explorer 9 supports the proper addEventListener method.
The addEventListener() method allows you to add event listeners on any HTML DOM object such as HTML elements, the HTML document, the window object, or other objects that support events, like the xmlHttpRequest object.
Quick answer: you have to use attachEvent if your browser returns undefined == window. addEventListener . Thing is the former is a non-standard JS function implemented in IE8 and previous versions, while addEventListener is supported by IE9+ (and all the other browsers).
addEventListener
is the proper DOM method to use for attaching event handlers.
Internet Explorer (up to version 8) used an alternate attachEvent
method.
Internet Explorer 9 supports the proper addEventListener
method.
The following should be an attempt to write a cross-browser addEvent
function.
function addEvent(evnt, elem, func) { if (elem.addEventListener) // W3C DOM elem.addEventListener(evnt,func,false); else if (elem.attachEvent) { // IE DOM elem.attachEvent("on"+evnt, func); } else { // No much to do elem["on"+evnt] = func; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With