Mar 26 2010

IE8 addEventListener Alternative Example

The W3C DOM Level 2 Event Model specifies addEventListener as the standard way to register event handlers but IE doesn’t support addEventListener. Instead IE provides an alternative attachEvent method for that purpose.

It does exactly the same thing but Microsoft decided to create their own method for the same purpose.

An example is given below shows how to provide cross browser compatibility for addEventListener:

someElement = document.getElementById('divName'); 


// for IE compatability

if (!someElement.addEventListener) {

    someElement.attachEvent("onclick", someFunction);


else {

    someElement.addEventListener("click", someFunction, false);


Please feel free to post other alternatives, if you know.

One Response to “IE8 addEventListener Alternative Example”

Leave a Reply