Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript AJAX Basics AJAX Concepts A Simple AJAX Example

Routine Poutine
Routine Poutine
26,038 Points

xhr.onreadystatechange vs. xhr.onload


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?



1 Answer

Steven Parker
Steven Parker
220,416 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?