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

async property (XMLDocument)

Browser support:
Specifies or retrieves whether downloading an XML file should be performed asynchronously or not.
An XML file can be downloaded with the load method of the XMLDocument object.
  • In case of synchronous loading, the load method does not return until the download is complete. After the load method returns and the returned value is true, you can start working with the downloaded XML file. See Example 1 for details.
  • In case of asynchronous loading, the load method starts the download in the background and returns. You must register an event handler to be informed about the state of the download. In Internet Explorer, use the onreadystatechange property, in Firefox, Opera, Google Chrome and Safari, use the addEventListener method to register the event handler for the 'load' event. See Example 2 for details.
The load method of the XMLDocument object is not supported by Google Chrome and Safari. If you need a cross-browser solution for downloading and opening XML files, use the XMLHttpRequest object. The open method of the XMLHttpRequest object is similar to the load method of the XMLDocument object.

Syntax:

object.async;
You can find the related objects in the Supported by objects section below.
This property is read/write.

Possible values:

Boolean, one of the following values:
false
The caller gets the control back only after the download is complete.
true
The caller gets the control back before the download is complete.
Default: this property has no default value.

Example HTML code 1:

This example shows how to use an external XML file to insert data into a table:
Code
ajax.js
news.xml
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        function FillTable () {
            var xmlDoc = CreateXMLDocumentObject ();    // defined in ajax.js
            if (!xmlDoc) {
                return;
            }

            if (typeof (xmlDoc.load) === "undefined") { // Google Chrome and Safari
                alert ("Your browser does not support the load method for XML documents!");
                return;
            }

            var url = "news.xml";
            xmlDoc.async = false;
            
            if (xmlDoc.load (url)) {
                var resTable = document.getElementById ("resTable");
                var xmlNodes = ["title", "description", "pubDate", "link"];

                var itemTags = xmlDoc.getElementsByTagName ("item");
                for (i = 0; i < itemTags.length; i++) {
                    resTable.insertRow (i);
                    for (j = 0; j < xmlNodes.length; j++) {
                        var recordNode = itemTags[i].getElementsByTagName (xmlNodes[j])[0];
                        resTable.rows[i].insertCell (j);
                        if ('textContent' in recordNode)
                            resTable.rows[i].cells[j].innerHTML = recordNode.textContent;
                        else
                            resTable.rows[i].cells[j].innerHTML = recordNode.text;
                    }
                }
            }
            else {
                    // display error message
                var errorMsg = null;
                if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
                    errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
                              + " at line " + xmlDoc.parseError.line
                              + " at position " + xmlDoc.parseError.linepos;
                }
                else {
                    if (xmlDoc.documentElement) {
                        if (xmlDoc.documentElement.nodeName == "parsererror") {
                            errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
                        }
                    }
                }
                if (errorMsg) {
                    alert (errorMsg);
                }
                else {
                    alert ("There was an error while loading XML file!");
                }
            }
        }
    </script>
</head>
<body onload="FillTable ()">
    <table border="1px">
        <thead style="font-weight: bold;">
            <tr>
                <td>Title</td>
                <td>Description</td>
                <td>PubDate</td>
                <td>Link</td>
            </tr>
        </thead>
        <tbody id="resTable">
        </tbody>
    </table>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example is similar to the previous one, but it uses asynchronous loading:
Code
ajax.js
news.xml
<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;
            }

            if (typeof (xmlDoc.load) === "undefined") { // Google Chrome and Safari
                alert ("Your browser does not support the load method for XML documents!");
                return;
            }

            var url = "news.xml";
            xmlDoc.async = true;
            if (xmlDoc.addEventListener) {  // all browsers except IE
                xmlDoc.addEventListener("load", OnLoadXML, false);
            }
            else {  // IE
                xmlDoc.onreadystatechange = OnStateChange;
            }
            xmlDoc.load (url);
        }

        function OnLoadXML () {
            FillTable ();
        }

        function OnStateChange () {
            if (xmlDoc.readyState == 0 || xmlDoc.readyState == 4) {
                FillTable ();
            }
        }

        function FillTable () {
            var errorMsg = null;
            if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
                errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
                          + " at line " + xmlDoc.parseError.line
                          + " at position " + xmlDoc.parseError.linepos;
            }
            else {
                if (xmlDoc.documentElement) {
                    if (xmlDoc.documentElement.nodeName == "parsererror") {
                        errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
                    }
                }
            }
            if (errorMsg) {
                alert (errorMsg);
                return null;
            }

            var resTable = document.getElementById ("resTable");
            var xmlNodes = ["title", "description", "pubDate", "link"];

            var itemTags = xmlDoc.getElementsByTagName ("item");
            for (i = 0; i < itemTags.length; i++) {
                resTable.insertRow (i);
                for (j = 0; j < xmlNodes.length; j++) {
                    var recordNode = itemTags[i].getElementsByTagName (xmlNodes[j])[0];
                    resTable.rows[i].insertCell (j);
                    if ('textContent' in recordNode)
                        resTable.rows[i].cells[j].innerHTML = recordNode.textContent;
                    else
                        resTable.rows[i].cells[j].innerHTML = recordNode.text;
                }
            }
        }
    </script>
</head>
<body onload="LoadXML ()">
    <table border="1px">
        <thead style="font-weight: bold;">
            <tr>
                <td>Title</td>
                <td>Description</td>
                <td>PubDate</td>
                <td>Link</td>
            </tr>
        </thead>
        <tbody id="resTable">
        </tbody>
    </table>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content