XML DOM Load Function
The code for loading an XML document can be stored in a function.
loadXMLDoc() Function
To make the code from the previous page easier to maintain (checking for old browsers), it should be written as a function:
loadxmldoc.js file code:
The function above can be stored in the <head> section of an HTML page, and called from a script in the page.
The functions described above are used for all XML document examples in this tutorial!
External JavaScript for loadXMLDoc()
To make the above code easier to maintain, and to ensure the same code is used on all pages, we store the function in an external file.
The file is named "loadxmldoc.js", and is loaded in the head section of an HTML page. Then, a script in the page calls the loadXMLDoc() function.
The following example uses the loadXMLDoc() function to loadbooks.xml:
Example
Try it Yourself »
How to obtain data from an XML file will be explained in the next chapter.
loadXMLString() function
To make the code from the previous page easier to maintain (checking for old browsers), it should be written as a function:
loadxmlstring.js file code:
The function above can be stored in the <head> section of an HTML page, and called from a script in the page.
The function described above is used in all XML string examples in this tutorial!
External JavaScript for loadXMLString()
We have stored the loadXMLString() function in a file named "loadxmlstring.js".
Example
Try it Yourself »
other extensions