From http://www.w3schools.com (Copyright Refsnes Data)
| « Previous | Next Chapter » |
The code for loading XML documents can be stored in a function.
To make the code from the previous page simpler to maintain (and check for older browsers), it should be written as a function:
|
function loadXMLDoc(dname) { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET",dname,false); xhttp.send(""); return xhttp.responseXML; } |
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 document examples in this tutorial!
To make the code above even easier to maintain, and to make sure the same code is used in all pages, we store the function in an external file.
The file is called "loadxmldoc.js", and will be loaded in the head section of an HTML page. Then, the loadXMLDoc() function can be called from a script in the page.
The following example uses the loadXMLDoc() function to load books.xml:
Example
Try it yourself » |
How to get the data from the XML file, will be explained in the next chapters.
To make the code from the previous page simpler to maintain (and check for older browsers), it should be written as a function:
|
function loadXMLString(txt) { if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); } else // Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.loadXML(txt); } return xmlDoc; } |
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!
We have stored the loadXMLString() function in a file called "loadxmlstring.js".
Example
Try it yourself » |
| « Previous | Next Chapter » |
From http://www.w3schools.com (Copyright Refsnes Data)