récuperer dans une méthode une variable d'une fonction
Posté : 25 févr. 2010, 15:50
Bonjour,
Je fais un script en javaScript qui via Ajax récupère une chaine de caractère via un script php/sql qui se connecte à mysql
La chaine est retournée et traitée via des regExps et les valeurs affichées via le DOM
Voici mon code :
et ma page ou le script est appelé ,c'est une sorte de méthode Main comme en java ...
tout fonctionne vu que j'ai fais le script en procédural et la je fais du refractoring en javaScript orienté objet
Donc mon soucis se situe au niveau de cette méthode:
this.req.onreadystatechange m'oblige à appeler une fonction
je suis pas loin du but retourner this.responseText à la méthode et initialiser this.load
Mais là je coince je n'arrive pas à l'aide d'un return à retouner quoique se soit ,faire une variable globale etc
Quelqu'un a t'il une idée Merci
Je fais un script en javaScript qui via Ajax récupère une chaine de caractère via un script php/sql qui se connecte à mysql
La chaine est retournée et traitée via des regExps et les valeurs affichées via le DOM
Voici mon code :
Code : Tout sélectionner
ObjectAjax=function(){
this.req=null;
if (window.ActiveXObject) {
this.req = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
this.req = new XMLHttpRequest();
this.req.overrideMimeType('text/xml');
}
}
ObjectAjax.prototype.exReq=function(){
this.load=null;//propriété issue de la fonction
this.req.open('GET','ajax.php',true);
this.req.onreadystatechange=function(){
var readyState = this.readyState;
if (readyState == 4) {
alert(this.responseText)
}
}
this.req.send(null);
}
ObjectAjax.prototype.returnDataBase=function() {
this.dataBase=dataBase=new Array();
var exReg=new RegExp("=[a-zA-Z0-9/.: ]*\]","g");//erreur caractere facultatif ignoré + pb <> extension jpg jpeg ou particularité javaScript/ajax
var chaine=this.texte.match(exReg).toString();
//alert(chaine);
var exReg2=new RegExp(",+","g");
var tab=chaine.split(exReg2);
//extraction des ids
//var exRegID=new RegExp("#[0-9]*#","g");
//var chaineID=str3.match(exRegID).toString();
//alert(tabID[i].substring(1,tabID[i].length-1));
var fraction = new Array();
var newtab = new Array();
//var tabID=chaineID.split(exReg2);
//var tableau = new Array();
for (var i=0; i<tab.length; i++) {
fraction[i]="'"+tab[i].substring(1,tab[i].length-1)+"'";
//ch="tableau"+i+"= new Array('"+fraction[i]+"');";
//eval(ch);
}
for (var j=0; j<18; j++) {
newtab[j]=fraction.slice(0,5);
var unechaine=newtab[j]+"";
ch="this.dataBase["+j+"]= new Array("+unechaine+");";
eval(ch);
fraction.splice(0,5);
}
}
ObjectAjax.prototype.createChampsJs=function() {
this.dataPiste=dataPiste=new Array();
this.dataGrandTitre=dataGrandTitre=new Array();
this.dataImage=dataImage=new Array();
this.dataMiniature=dataMiniature=new Array();
for (var k=0; k<18; k++) {
//alert(dataBase[k][1]);
this.dataPiste[this.dataBase[k][1]]=this.dataBase[k][1];
this.dataGrandTitre[this.dataBase[k][2]]=this.dataBase[k][2];
this.dataImage[this.dataBase[k][3]]=this.dataBase[k][3];
this.dataMiniature[this.dataBase[k][4]]=this.dataBase[k][4];
}
}
ObjectAjax.prototype.tabNamePiste=function() {
this.TabPisteValue=TabPisteValue=new Array();
this.indice=indice=0;
for(var val in this.dataPiste){
this.indice++;
this.TabPisteValue[this.indice]=this.dataPiste[val];
}
}
ObjectAjax.prototype.createDataBaseJs=function(){
this.myDataBase=myDataBase=new Array;
for (var id=0; id<18; id++) {
for(var numCh=0;numCh<5;numCh++){
this.myDataBase[id]=new Array(this.dataBase[id][0],this.dataBase[id][1],this.dataBase[id][2],this.dataBase[id][3],this.dataBase[id][4]);
}
}
}
function static_void_Main(){
var obAjax = new ObjectAjax();
obAjax.exReq();
//obAjax.returnDataBase();
/*obAjax.createChampsJs();
obAjax.createDataBaseJs();
obAjax.tabNamePiste();
obAjax.IdPosition();
obAjax.afficheTableauAccueil();
obAjax.clickUrl;*/
}
//newtimer = setTimeout("static_void_Main()",1000);
window.onload=static_void_Main;
tout fonctionne vu que j'ai fais le script en procédural et la je fais du refractoring en javaScript orienté objet
Donc mon soucis se situe au niveau de cette méthode:
Code : Tout sélectionner
ObjectAjax.prototype.exReq=function(){
this.load=null;//propriété issue de la fonction
this.req.open('GET','ajax.php',true);
this.req.onreadystatechange=function(){
var readyState = this.readyState;
if (readyState == 4) {
alert(this.responseText)//<--- retourner cette variable là à la méthode
}
}
this.req.send(null);
}
je suis pas loin du but retourner this.responseText à la méthode et initialiser this.load
Mais là je coince je n'arrive pas à l'aide d'un return à retouner quoique se soit ,faire une variable globale etc
Quelqu'un a t'il une idée Merci