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 thought on “IE8 addEventListener Alternative Example

  1. Thanks for sharing this! I wasn’t aware of IE8’s shortcomings until a client started testing some code, once found I needed a quick fix. This worked for me.

    Gerard

Leave a Reply

Your email address will not be published. Required fields are marked *