Hallo liebe Runde,
mit Hilfe eines XMLHttpRequests soll ein Select befüllt werden:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>AJAX Test</title>
<script>
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
function PopulateSelect(theSelect, theElement)
{
xmlhttp.open("GET", "test.xml",true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
var nodeLength = xmlhttp.responseXML.getElementsByTagName(theElement).length;
alert(nodeLength);
for(var i = 0; i < nodeLength; i++)
{
newEntry = new Option(xmlhttp.responseXML.getElementsByTagName(theElement)[i].childNodes[0].nodeValue,1,false,true);
document.getElementById(theSelect).options[document.getElementById(theSelect).length] = newEntry;
}
document.getElementById(theSelect).options[0].selected = true;
}
}
xmlhttp.send(null)
}
</script>
</head>
<body>
<a href="#" onclick="PopulateSelect('testSelect','vorname');">test populate Select</a><br>
<select id="testSelect" size="1"></select>
</body>
</html>
Der Call importiert dieses XML-Dokument:
<?xml version="1.0"?>
<humans>
<person>
<vorname>Max</vorname>
<nachname>Meier</nachname>
</person>
<person>
<vorname>Peter</vorname>
<nachname>Mueller</nachname>
</person>
<person>
<vorname>Sonja</vorname>
<nachname>Schuster</nachname>
</person>
</humans>
Während Firefox und Konsorten einwandfrei die Länge 3 des getElementsByTagName - Objektes ausgeben und in Folge das Select mit drei Einträgen befüllen, "sieht" der IE nur eine Länge 1 und dementsprechend einen Eintrag.
Gibt es spezifische Unterschiede der getElementsByTagName-Methode bei Verwendung in einem xmlhttp-Request?
Danke für jeden Tipp!
Gruss
Marcus