Ich poste nochmal den ganzen Code:
function Chat(i) {
this.interlocutor = i;
this.httpObject = null;
this.link = "";
this.timerID = 0;
var objChat = this;
//this.UpdateTimer();
this.doReload();
//document.getElementById("send_"+this.interlocutor).onclick = objChat.doWork();
//document.getElementById("sender_"+this.interlocutor).onkeyup = objChat.keypressed(event);
}
// Get the HTTP Object
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
};
// Change the value of the outputText field
Chat.prototype.setOutput = function (){
if(this.httpObject.readyState == 4){
var response = this.httpObject.responseText;
var objDiv = document.getElementById("result_"+this.interlocutor);
objDiv.innerHTML = response;
objDiv.scrollTop = objDiv.scrollHeight;
var inpObj = document.getElementById("msg_"+this.interlocutor);
inpObj.value = "";
inpObj.focus();
}
};
// Change the value of the outputText field
Chat.prototype.setAll = function (){
var objChat = this;
if(objChat.httpObject.readyState == 4){
var response = this.httpObject.responseText;
var objDiv = document.getElementById("result_"+this.interlocutor);
objDiv.innerHTML = response;
objDiv.scrollTop = objDiv.scrollHeight;
}
};
// Implement business logic
Chat.prototype.doWork = function (){
this.httpObject = getHTTPObject();
if (this.httpObject != null) {
var objChat = this;
this.link = "http://localhost/src/message.php?interlocutor="+this.interlocutor+"&msg="+document.getElementById('msg_'+this.interlocutor).value;
this.httpObject.open("GET", this.link , true);
this.httpObject.onreadystatechange = objChat.setOutput;
this.httpObject.send(null);
}
};
// Implement business logic
Chat.prototype.doReload = function (){
this.httpObject = getHTTPObject();
if (this.httpObject != null) {
var objChat = this;
this.link = "http://localhost/src/message.php?interlocutor="+this.interlocutor;
this.httpObject.open("GET", this.link , true);
this.httpObject.onreadystatechange = objChat.setAll;
this.httpObject.send(null);
}
};
Chat.prototype.UpdateTimer = function () {
var objChat = this;
this.doReload();
this.timerID = setTimeout(objChat.UpdateTimer(), 5000);
};
Chat.prototype.keypressed = function (e){
if(e.keyCode=='13'){
this.doWork();
}
};