I am using XMLHttpRequest to request an XML file and display the data in an HTML page. I would like to call upon multiple XML files in the same HTML document without writing a different loadXMLDoc() function for each file that I will need to request. This more of a question on Javascript fundamentals.
How can I request multiple XML files using XMLHttpRequest on the same page while using the same function (loadXMLDoc) to do it? Perhaps using 'this' keyword or creating a class? Both are new to me, so I'm not sure.
<html>
<head>
<script type="text/javascript" >
var xmlhttp
function loadXMLDoc(url)
{
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
//alert("mozilla");
xmlhttp=new XMLHttpRequest()
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send()
}
}
}
function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
displayLibrary();
}
else
{
alert("Problem retrieving XML data:" + xmlhttp.statusText)
}
}
}
function getElementTextNS(prefix, local, parentElem, index) {
var result = "";
if (prefix && isIE) {
// IE/Windows way of handling namespaces
result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
} else {
result = parentElem.getElementsByTagName(local)[index];
}
if (result) {
// get text, accounting for possible
// whitespace (carriage return) text nodes
if (result.childNodes.length > 1) {
return result.childNodes[1].nodeValue;
} else {
return result.firstChild.nodeValue;
}
} else {
return "n/a";
}
}
function displayLibrary() {
//loops through xml, builds a table and inserts table into Library div
}
function displayCategories() {
//loops through xml, builds a table and inserts table into Categories div
}
loadXMLDoc('Library.xml');
//loadXMLDoc('Categories.xml');
</script>
</head>
<body>
<table width="760" align="center">
<tr>
<td colspan="3"><H3>HEADER</H3></td>
</tr>
<tr>
<td valign="top"><DIV id="Categories"></DIV></td>
<td><DIV id="Library"></DIV></td>
<td>Other Content</td>
</tr>
<tr>
<td colspan="3" align="center">FOOTER</td>
</tr>
</table>
</body>
</html>
How can I request multiple XML files using XMLHttpRequest on the same page while using the same function (loadXMLDoc) to do it? Perhaps using 'this' keyword or creating a class? Both are new to me, so I'm not sure.
<html>
<head>
<script type="text/javascript" >
var xmlhttp
function loadXMLDoc(url)
{
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
//alert("mozilla");
xmlhttp=new XMLHttpRequest()
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send()
}
}
}
function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
displayLibrary();
}
else
{
alert("Problem retrieving XML data:" + xmlhttp.statusText)
}
}
}
function getElementTextNS(prefix, local, parentElem, index) {
var result = "";
if (prefix && isIE) {
// IE/Windows way of handling namespaces
result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
} else {
result = parentElem.getElementsByTagName(local)[index];
}
if (result) {
// get text, accounting for possible
// whitespace (carriage return) text nodes
if (result.childNodes.length > 1) {
return result.childNodes[1].nodeValue;
} else {
return result.firstChild.nodeValue;
}
} else {
return "n/a";
}
}
function displayLibrary() {
//loops through xml, builds a table and inserts table into Library div
}
function displayCategories() {
//loops through xml, builds a table and inserts table into Categories div
}
loadXMLDoc('Library.xml');
//loadXMLDoc('Categories.xml');
</script>
</head>
<body>
<table width="760" align="center">
<tr>
<td colspan="3"><H3>HEADER</H3></td>
</tr>
<tr>
<td valign="top"><DIV id="Categories"></DIV></td>
<td><DIV id="Library"></DIV></td>
<td>Other Content</td>
</tr>
<tr>
<td colspan="3" align="center">FOOTER</td>
</tr>
</table>
</body>
</html>