pedroyanky pedroyanky - 6 months ago 20
Javascript Question

what does this add atachment do in javascript addEventListener

Please am having a little difficulty to understand exactly what is the meaning of this attachment in this statements

var x = document.getElementById('myBtn');
if (x.addEventListner) {
//for all major browsers except IE 8 and earlier
x.addEventlistener('click', myFunction);
} else if (x.attachEvent) {
//for IE 8 and earlier versions
x.attachEvent('onclick', myFunction);

Can anybody tell me what this attachment stuff is all about? I did know addEventListener, but not attachEvent.


Non-standard - This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

This is a proprietary Microsoft Internet Explorer alternative to the standard EventTarget.addEventListener() method.

enter image description here


The addEventListener() method is not supported in Internet Explorer 8 and earlier versions, and Opera 6.0 and earlier versions. This is what your if statement is validating, to be able to support the other browsers.