I have a script that loads info from external XML file and inserts it into the HTML page on click. Here's the code of this script:
var xmlhttp;
function loadXMLDoc(url,target) {
xmlhttp = null;
if (window.XMLHttpRequest) { // code for all new browsers
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // code for IE5 and IE6
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
xmlhttp.onreadystatechange = state_Change;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}
else {
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change() {
if (xmlhttp.readyState == 4) { // 4 = "loaded"
if (xmlhttp.status == 200) { // 200 = OK
var markers = xmlhttp.responseXML.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getElementsByTagName("name")[0].firstChild.nodeValue;
//etc...
document.getElementById(target).innerHTML += '<li>' + name + '</li>\n';
}
}
else {
alert("Problem retrieving XML data");
}
}
}
Here's the HTML:
<ul id="list_puncts">
<li><a href="javascript:;" onclick="loadXMLDoc('./content/geo_points/slovenia.xml','list_sl')">Republika Slovenija (RS)</a>
<ul id="list_sl">
<!--here should be some info from XML file-->
</ul></li>
<li><a href="javascript:;" onclick="loadXMLDoc('./content/geo_points/horvatia.xml','list_hr')">Republika Hrvatska (RH)</a>
<ul id="list_hr">
<!--here should be some info from XML file-->
</ul></li>
</ul>
However, it does not work - after the link is clicked, XML gets loaded (it can be seen in Firebug), but the second variable - target
- cannot make its way into the state_Change
function, so no real action is done. If target
in
document.getElementById(target).innerHTML
is replaced by some static id (like list_sl
), it is working, but I have many of these links in the HTML, not only Slovenia and Horvatia, so the variable is strongly needed.
Thanks for any help.