javascript – Why not use addEventListener for onreadystatechange

Question: Question:

readystatechange is an event, I think it can be registered using addEventListener , but most Ajax samples use the method of assigning to onreadystatechange .

Is it also a reason to avoid something? Is it a little long?

var xhr = new XMLHttpRequest();
xhr.open("GET", "/path/to/file", true);
xhr.onreadystatechange = function(){
  if( this.readyState === 4 && this.status === 200 ){
    console.log(xhr.responseText);
  }
};
xhr.addEventListener('readystatechange', function(){
  if( this.readyState === 4 && this.status === 200 ){
    console.log(xhr.responseText);
  }
});
xhr.send("");

Answer: Answer:

(I don't know about it now) addEventListener doesn't seem to work in Opera.
According to the W3C specifications, it is necessary to fire a ready state change, but it seems that it does not follow it.

I had a crazy question ↓

https://stackoverflow.com/questions/6971259/readystatechange-using-addeventlistener-versus-old-style-property

Scroll to Top