Why don't you use addEventListener for onlinestatechange?

Asked 2 years ago, Updated 2 years ago, 117 views

readystatechange is an event, so I think I can register using addEventListener, but I think most Ajax samples use the method of replacing it with readystatechange.

Is there any reason to avoid it?Because it's a bit 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("");

javascript ajax

2022-09-30 18:19

1 Answers

(I don't know what it is right now, but) The addEventListener doesn't seem to work with Opera.
According to the W3C specification, the readystatechange needs to be ignited, but it doesn't seem to follow it.

I had a crazy question↓

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


2022-09-30 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.