Ajax > onreadystatechange - open/send > order totally arbitrary?

送分小仙女□ 提交于 2019-12-03 21:24:01

Generally speaking, if the only readyState that you care about is 4, then it doesn't really make a difference if the onreadystatechange event handler is assigned prior to calling open(), in between open() and send(), or after calling send(). Here are the possible values for readyState:

  • 0 - Uninitialized. The open() method hasn’t been called yet.
  • 1 - Open. The open() method has been called but send() has not been called.
  • 2 - Sent. The send() method has been called but no response has been received.
  • 3 - Receiving. Some response data has been retrieved.
  • 4 - Complete. All of the response data has been retrieved and is available.

By defining onreadystatechange before the open method is invoked, it is able to detect every state change from 0 through 4. If it is defined after the open method, then only states 1 through 4 will be detected. For this reason, it is generally preferred to place the onreadystatechange assignment before open().

One caveat to note is that when onreadystatechange was introduced in Internet Explorer 7, you had to set the event handler after calling open, otherwise it would cause an error. But this has been fixed in later versions of IE.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!