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.

Syntax:

object.readyState;
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:
0
The current object is not initialized (the open method has not been called yet).
1
The request is opened, but the send method has not been called yet.
2
The request is sent but no data has been received yet.
3
A part of the data has been received, but it is not yet available.
4
All data is available.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the readyState property:
Code
ajax.js
response.txt
<head>
    <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";
                httpRequest.open ("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 />";
                break;
            case 2:
                output.innerHTML += "The request is sent but no data has been received yet.<br />";
                break;
            case 3:
                output.innerHTML += "A part of the data has been received.<br />";
                break;
            case 4:
                output.innerHTML += "All data has been received:<br /><span style='margin-left:30px; color:blue'>" + httpRequest.responseText + "</span><br />";
                break;
            };
        }

    </script>
</head>
<body>
    <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>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content