Ciao. Sto facendo una chat in AJAX, con javascript, xml e asp.
Conosco asp e un poco javascript, però è la prima volta che ho a che fare con xml e ajax.
Qui (http://livelogin.brinkster.net/chat/default.asp) potete vedere i risultati dei miei sforzi.
Ho risolto da solo innumerevoli problemi di xml, ora ci sono due cose che non riesco a sistemare:
1) lo scroll fino in fondo. Quando si supera la lunghezza della finestra dovrebbe scendere per visualizzare l'ultimo testo scritto. Il problema è che lo fa, ma solo fino al penultimo! Non scende fino all'ultima riga!
2) ovviamente manca la cosa principale: il reload. Se io scrivo, mi ricarica il file xml, però deve farlo anche quando scrive l'altro. L'unico modo penso sia ricaricare il file xml ogni tot di tempo. Ho provato a mettere un setInterval ma si impalla tutto.

Qualcuno sa darmi una mano? Grazie.

Il codice (in gran parte preso da html.it):

codice:
<script type="text/javascript">

// funzione per prendere un elemento con id univoco
function prendiElementoDaId(id_elemento) {
	var elemento;
	if(document.getElementById)
		elemento = document.getElementById(id_elemento);
	else
		elemento = document.all[id_elemento];
	return elemento;
};

// funzione per assegnare un oggetto XMLHttpRequest
function assegnaXMLHttpRequest() {
	var
		XHR = null,
		browserUtente = navigator.userAgent.toUpperCase();
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
		XHR = new XMLHttpRequest();
	else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
		if(browserUtente.indexOf("MSIE 5") < 0)
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		else
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return XHR;
};	


/** OGGETTI / ARRAY */

// oggetto di verifica stato
var readyState = {
	INATTIVO:	0,
	INIZIALIZZATO:	1,
	RICHIESTA:	2,
	RISPOSTA:	3,
	COMPLETATO:	4
};

// array descrittivo dei codici restituiti dal server
// [la scelta dell' array è per evitare problemi con vecchi browsers]
var statusText = new Array();
statusText[100] = "Continue";
statusText[101] = "Switching Protocols";
statusText[200] = "OK";
statusText[201] = "Created";
statusText[202] = "Accepted";
statusText[203] = "Non-Authoritative Information";
statusText[204] = "No Content";
statusText[205] = "Reset Content";
statusText[206] = "Partial Content";
statusText[300] = "Multiple Choices";
statusText[301] = "Moved Permanently";
statusText[302] = "Found";
statusText[303] = "See Other";
statusText[304] = "Not Modified";
statusText[305] = "Use Proxy";
statusText[306] = "(unused, but reserved)";
statusText[307] = "Temporary Redirect";
statusText[400] = "Bad Request";
statusText[401] = "Unauthorized";
statusText[402] = "Payment Required";
statusText[403] = "Forbidden";
statusText[404] = "Not Found";
statusText[405] = "Method Not Allowed";
statusText[406] = "Not Acceptable";
statusText[407] = "Proxy Authentication Required";
statusText[408] = "Request Timeout";
statusText[409] = "Conflict";
statusText[410] = "Gone";
statusText[411] = "Length Required";
statusText[412] = "Precondition Failed";
statusText[413] = "Request Entity Too Large";
statusText[414] = "Request-URI Too Long";
statusText[415] = "Unsupported Media Type";
statusText[416] = "Requested Range Not Satisfiable";
statusText[417] = "Expectation Failed";
statusText[500] = "Internal Server Error";
statusText[501] = "Not Implemented";
statusText[502] = "Bad Gateway";
statusText[503] = "Service Unavailable";
statusText[504] = "Gateway Timeout";
statusText[505] = "HTTP Version Not Supported";
statusText[509] = "Bandwidth Limit Exceeded";

var source = 'chat.asp?rdm=';
var input;

function leggi(){
	source+= Math.random();
	caricaXML(source);				
	input = prendiElementoDaId('testo');
	input.focus();
	scroll();
	//timerID=setInterval('leggi()',2000); 

}

function scroll(){
	var chat = prendiElementoDaId('chat');
	chat.scrollTop = chat.scrollHeight;					
}

function scrivi(testo){
	if(testo!=""){
		source+= Math.random();
		var today = new Date();
		var hour = today.getHours() + "." + today.getMinutes();
		caricaXML(source + "&text=" + testo + "&hour=" + hour);
		input.value = "";
		input.focus();
		scroll();
	}
}

function caricaXML(nomeFile) {
	var 	ajax = assegnaXMLHttpRequest(),
		elemento = prendiElementoDaId("chat");
	if(ajax) {
		ajax.open("get", nomeFile, true);
		ajax.setRequestHeader("connection", "close");
		ajax.onreadystatechange = function() {
			if(ajax.readyState === readyState.COMPLETATO) {
				if(statusText[ajax.status] === "OK") {
					if(ajax.responseXML)
						elemento.innerHTML = parsaXml(ajax.responseXML);
					
					else
						elemento.innerHTML =
						"L'XML restituito dalla richiesta non è valido.
" +
						ajax.responseText.split('<').join("&lt;").split('>').join("&gt;");
				}					
				else
					elemento.innerHTML =
					"Impossibile effettuare l'operazione richiesta.
" +
					"Errore riscontrato: " + statusText[ajax.status];
			};
		};
		ajax.send(null);
	};
};

function parsaXml(xml) {
	var time = xml.getElementsByTagName("item/time");
	var from = xml.getElementsByTagName("item/from");
	var testo = xml.getElementsByTagName("item/text");
	risultato = "";

	for(var i = 0; i < time.length; i++) {
		risultato +=
		"<div><span class=\"time\">"+ time[i].text + 
		"</span> - <span class=\"nick\">" + from[i].text + 
		"</span> scrive: <span class=\"text\">" + testo[i].text +
		"</span></div>";
	};
	return risultato;
};

</script>
codice:
<body onload="leggi();">
<h1>LIVE CHAT</h1>
<div id="chat"></div>
<div>
	<form onsubmit="scrivi(prendiElementoDaId('testo').value);return false;">
		<input type="text" name="testo" id="user" />

		<input type="submit" value="INVIA"><input type="button" value="Esci" onclick="window.location.href='default.asp?action=logout'">
	</form>
</div>
</body>