Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addEventListener in Internet Explorer

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.

like image 774
The Mask Avatar asked Aug 03 '11 13:08

The Mask


People also ask

Is addEventListener supported by IE?

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.

What is an addEventListener?

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.

What is the difference between addEventListener and attachEvent?

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).


1 Answers

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;    } } 
like image 182
user278064 Avatar answered Oct 08 '22 23:10

user278064