Can one replace xhr.onreadystatechange with xhr.onload for AJAX calls?

前端 未结 4 1021
星月不相逢
星月不相逢 2020-12-08 06:30

I need to support major modern browsers only (IE10+, FF, Chrome, Safari)

Can I make this substitution as I want to simplify my code base:

From:

4条回答
  •  一个人的身影
    2020-12-08 06:49

    In the MDN documentation they state the following

    Events

    onreadystatechange as a property on the xhr object is supported in all browsers.

    Since then, a number of additional event handlers were implemented in various browsers (onload, onerror, onprogress, etc.). These are supported in Firefox. In particular, see nsIXMLHttpRequestEventTarget and Using XMLHttpRequest.

    More recent browsers, including Firefox, also support listening to the XMLHttpRequest events via standard addEventListener APIs in addition to setting on* properties to a handler function.

    So I think you can assume that onreadystatechange is the way to go and onload is a addition that can be used if the browser supports it. @mr.VVoo answer is the correct one, to stick to w3c when in doubt.

提交回复
热议问题