PDA

View Full Version : [Javascript] Classi


max246
08-05-2007, 08:31
Ho un problema con una classe JS che non riesco a far funzionare...
function HtmlRequest (url) {
this.objHTTP = CreaOggettoHTTP();
this.url = url;
this.method = "GET";
this.InviaRichiesta = InviaRichiesta;
this.Operazione = Operazione;
function CreaOggettoHTTP() {
if (navigator.appName == "Microsoft Internet Explorer") {
var objHttp = new ActiveXObject("Microsoft.XMLHTTP");
ie = true;
} else {
var objHttp = new XMLHttpRequest();
}
return objHttp;
}
function InviaRichiesta() {
this.objHTTP.open(this.method,this.url,true);
this.objHTTP.onreadystatechange = Operazione;
this.objHTTP.send(null);
}
function Operazione() {
//objHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//objHttp.send("search_what=All");
alert(this.objHTTP.readyState);
if (this.objHTTP.readyState == 4) {

}
}


}
var a = new HtmlRequest("index.php");
a.InviaRichiesta();

Ma mi ritorna questo errore ... aiuto!!!
this.objHTTP has no properties
http://www.xxxxxxxxx/test.php
Line 33

AlexGatti
09-05-2007, 12:18
in CreaOggettoHTTP
function CreaOggettoHTTP() {
if (navigator.appName == "Microsoft Internet Explorer") {
var objHttp = new ActiveXObject("Microsoft.XMLHTTP");
ie = true;
} else {
var objHttp = new XMLHttpRequest();
}
return objHttp;
}

fai la dichiarazione di objHttp dentro all'if, fuori, nel return non so se l'oggetto dichiarato dentro č ancora attivo.
insomma, io la farei cosė:
function CreaOggettoHTTP() {
var objHttp=0;
if (navigator.appName == "Microsoft Internet Explorer") {
objHttp = new ActiveXObject("Microsoft.XMLHTTP");
ie = true;
} else {
objHttp = new XMLHttpRequest();
}
return objHttp;
}