Property | Description |
---|---|
onreadystatechange | Used as an event handler for events that trigger upon state changes |
readyState | Contains the current state of the object (0: uninitialized, 1: loading, 2: loaded, 3: interactive, 4: complete) |
responseText | Returns the response in string format |
responseXML | Returns the response in proper XML format |
status | Returns the status of the request in numerical format (regular page errors are returned, such as the number 404, which refers to a not found error) |
statusText | Returns the status of the request, but in string format (e.g., a 404 error would return the string Not Found) |
onreadystatechange
The onreadystatechange property is an event handler that allows you to trigger certain blocks of code, or functions, when the state (referring to exactly where the process is at any given time) changes. For example, if you have a function that handles some form of initialization, you could get the main set of functionality you want to fire as soon as the state changes to the complete state.
readyState
The readyState property gives you an in-depth description of the part of the process that the current request is at. This is a highly useful property for exception handling, and can be important when deciding when to perform certain actions. You can use this property to create individual actions based upon how far along the request is. For example, you could have a set of code execute when readyState is loading, or stop executing when readyState is complete.
responseText
The responseText property will be returned once a request has gone through. If you are firing a request to a script of some sort, the output of the script will be returned through this property. With that in mind, most scripts will make use of this property by dumping it into an innerHTML property of an element, thereby asynchronously loading a script or document into a page element.
responseXML
This works similarly to responseText, but is ideal if you know for a fact that the response will be returned in XML format—especially if you plan to use built-in XML-handling browser functionality.
status
This property dictates the response code (a list of common response codes is shown in Table 2-1) that was returned from the request. For instance, if the file requested could not be found, the status will be set to 404 because the file could not be found.
statusText
This property is merely a textual representation of the status property. Where the status property might be set to 404, the statusText would return Not Found. By using both the status and statusText properties together, you can give your user more in-depth knowledge of what has occurred. After all, not many users understand the significance of the number 404.