Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics AJAX Concepts A Simple AJAX Example

Routine Poutine
Routine Poutine
26,050 Points

xhr.onreadystatechange vs. xhr.onload

Hi,

Are these two event handlers on the xhr object the same?

a. xhr.onreadystatechange b. xhr.onload

I thought xhr.onreadystatechange also required this.status === 200, but xhr.onload only required this.readyState === 4. In this video they appear to be the same. Can I use xhr.onload for brevity?

Best,

Matt

1 Answer

Steven Parker
Steven Parker
231,269 Points

A readystatechange occurs several times in the life of a request as it progresses to different phases, but a load event only occurs when the request has successfully completed.

If you're not interested in detecting intermediate states or errors, then onload might be a good choice.

Hey Steve! Wouldn't onloadend make more sense than onload in that situation?