You are here: Reference > JavaScript > client-side > xml handling > properties > readyState (XMLHttpRequest)

readyState property (XMLHttpRequest)

Browser support:
Returns an integer value that indicates the state of the request operation.
Use this property together with the onreadystatechange event to be notified when the state changes.
The onreadystatechange event and the readyState property is useful if the request opened by the open method needs to be handled asynchronously.


You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Integer that represents the state of the object.
One of the following values:
The current object is not initialized (the open method has not been called yet).
The request is opened, but the send method has not been called yet.
The request is sent but no data has been received yet.
A part of the data has been received, but it is not yet available.
All data is available.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the readyState property:
    <script type="text/javascript" src="ajax.js"></script>
    <script type="text/javascript">
        var httpRequest = null;

        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = "response.txt";
       ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);

        function OnStateChange () {
            var output = document.getElementById ("output");
            switch (httpRequest.readyState) {
            case 1:
                output.innerHTML += "The request is opened, but the send method has not been called yet.<br />";
            case 2:
                output.innerHTML += "The request is sent but no data has been received yet.<br />";
            case 3:
                output.innerHTML += "A part of the data has been received.<br />";
            case 4:
                output.innerHTML += "All data has been received:<br /><span style='margin-left:30px; color:blue'>" + httpRequest.responseText + "</span><br />";

    <button onclick="SendRequest ()">Test readyState property</button>
    <br /><br />
    <div id="output" style="width:420px; height:200px; overflow:auto; border:1px solid #000000; background-color:#f5e4b1;"></div>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content