Name

XMLHttpRequest.onreadystatechange: event handler function invoked when readyState changes

Synopsis

Function onreadystatechange

Description

This property specifies an event-handler function that is invoked each time the readyState property changes. It may also be invoked (but this is not required) multiple times while readyState is 3 to provide notification of download progress.

An onreadystatechange handler typically checks the readyState of the XMLHttpRequest object to see if it has reached 4. If so, it does something with the responseText or responseXML properties.

It is unspecified whether any arguments will be passed to the function. In particular, there is no standard way for the event-handler function to get a reference to the XMLHttpRequest object it is registered on. This means that it is not possible to write a generic handler function that can be used for multiple requests.

The XMLHttpRequest object is supposed to follow the DOM event model and implement an addEventListener( ) method for registering handlers for readystatechange events. (See Event.addEventListener( ), for example.) Since IE does not support the DOM event model, and since it is rare to require more than one event handler per request, it is safer to simply assign a single handler function to onreadystatechange.

Get JavaScript: The Definitive Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.