Vi spiego, sto leggendo Ajax for Dummies e stavo giocando un po' con i vari listati che usa. Ora sono alla manipolazione di file xml ma c'è qualcosa che non mi torna. Il codice l'ho copiato dal libro quindi non so dove possa aver sbagliato.
file: guest.html
codice:
<html>
<head>
<title>Using Ajax and XML</title>
<script language = “javascript”>
function getGuest() {
var mozillaFlag = false;
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
XMLHttpRequestObject.overrideMimeType(“text/xml”);
mozillaFlag = true;
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new
ActiveXObject(“Microsoft.XMLHTTP”);
}
if (XMLHttpRequestObject) {
XMLHttpRequestObject.open(“GET”, “guests.xml”, true);
XMLHttpRequestObject.onreadystatechange = function() {
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
var xmlDocument = XMLHttpRequestObject.responseXML;
if(mozillaFlag) {
removeWhitespace(xmlDocument);
}
displayGuest(xmlDocument);
}
}
XMLHttpRequestObject.send(null);
}
}
function displayGuest (xmldoc) {
firstnamenodes = xmldoc.getElementsByTagName(“first_name”);
lastnamenodes = xmldoc.getElementsByTagName(“last_name”);
var displayText = “The main guest was: “ +
firstnamenodes[2].firstChild.nodeValue + ‘ ‘
+ lastnamenodes[2].firstChild.nodeValue;
var target = document.getElementById(“targetDiv”);
target.innerHTML=displayText;
}
function removeWhitespace(xml) {
var loopIndex;
for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++) {
var currentNode = xml.childNodes[loopIndex];
if (currentNode.nodeType == 1) {
removeWhitespace(currentNode);
}
if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) {
xml.removeChild(xml.childNodes[loopIndex--]);
}
}
}
</script>
</head>
<body>
<h1>Using Ajax and XML</h1>
<form>
<input type=submit onclick=getGuest()>
</form>
<div id=”targetDiv” width =100 height=100>
Who was the main guest?
</div>
</body>
</html>
Ed ecco il file: guests.xml
codice:
<?xml version=”1.0”?>
<events>
<event type=”informal”>
<event_title>15th award ceremony</event_title>
<event_number>1207</event_number>
<subject>gala event</subject>
<date>7/4/2006</date>
<people>
<person attendance=”present”>
<first_name>Sam</first_name>
<last_name>Edwards</last_name>
</person>
<person attendance=”absent”>
<first_name>Sally</first_name>
<last_name>Jackson</last_name>
</person>
<person attendance=”present”>
<first_name>Cary</first_name>
<last_name>Grant</last_name>
</person>
</people>
</event>
</events>
Non riesco a capire dove sia l'errore, ma quando clicco il bottone non succede un bel niente.
Qui la prova: http://mariano.altervista.org/ajax/cartella/guests.html
Grazie a tutti.