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

readyState property (XMLDocument)

Browser support:
Returns an integer value that indicates the load state of the XMLDocument object.
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 load method loads an XML document asynchronously. If you want to start processing data as soon as it becomes available, use the ondataavailable event.

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:
1
The load method was called, but no data has been received yet.
2
A part of the data has been loaded, but the document object model (DOM) is not yet available.
3
The document object model has been built from the received data and it is available.
4
All data has been loaded, the operation is finished.
Default: this property has no default value.

Example HTML code 1:

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

        function LoadXML () {
            xmlDoc = CreateXMLDocumentObject ();    // defined in ajax.js
            if (!xmlDoc) {
                return;
            }

            var url = "large.xml";
            xmlDoc.async = true;

            if (xmlDoc.addEventListener) {  // Firefox, Opera, Google Chrome and Safari
                xmlDoc.addEventListener("load", OnLoadXML, false);
            }
            else {
                xmlDoc.onreadystatechange = OnStateChange;
            }

            xmlDoc.load (url);
        }

        function OnLoadXML () {
            LoadingFinished ();
        }

        function OnStateChange () {
            var output = document.getElementById ("output");
            switch (xmlDoc.readyState) {
            case 1:
                output.innerHTML += "The load method was called, but no data has been received yet.<br/>";
                break;
            case 2:
                output.innerHTML += "A part of the data has been loaded, but the document object model (DOM) is not yet available.<br/>";
                break;
            case 3:
                output.innerHTML += "The document object model has been built from the received data and it is available.<br/>";
                break;
            case 4:
                LoadingFinished ();
                break;
            };
        }

        function LoadingFinished () {
            var output = document.getElementById ("output");
            output.innerHTML += "All data has been loaded, the operation is finished:<br/>";

            var errorMsg = null;
            if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
                errorMsg = "XML Parsing Error: <span style='color:red'>" + xmlDoc.parseError.reason
                          + " at line " + xmlDoc.parseError.line
                          + " at position " + xmlDoc.parseError.linepos
                          + "</span><br/>";
            }
            else {
                if (xmlDoc.documentElement) {
                    if (xmlDoc.documentElement.nodeName == "parsererror") {
                        errorMsg = "<span style='color:red'>" + xmlDoc.documentElement.childNodes[0].nodeValue + "</span><br/>";
                    }
                }
            }
            if (errorMsg) {
                output.innerHTML += errorMsg;
            }
            else {
                output.innerHTML += "<span style='color:blue'>The XML is valid.</span><br/>";
            }
        }
    </script>
</head>
<body>
    <button onclick="LoadXML ()">Analyze the loading process of an XML</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