Define variables to call a function

2

I'm trying to include variables to call a function, but I'm not getting it

function Geral()
{ 
    xmlHttp=GetXmlHttpObject();
    var url="pagina.asp?a=1";
    xmlHttp.onreadystatechange=stateChanged; 
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateChanged() 
{ 
    if (xmlHttp.readyState==4)
    { 
    document.getElementById("dados").innerHTML=xmlHttp.responseText;
    newTag(dados);
    }
}

I did this, but it did not work:

Geral("pagina.asp?a=1","dados",dados)
function Geral(PAGINA,DADOS,TAG)
{ 
    xmlHttp=GetXmlHttpObject();
    var url="PAGINA";
    xmlHttp.onreadystatechange=stateChanged(DADOS,TAG);
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateChanged(A,B) 
{ 
    if (xmlHttp.readyState==4)
    { 
    document.getElementById(A).innerHTML=xmlHttp.responseText;
    newTag(B);
    }
}

What's wrong?

    
asked by anonymous 26.10.2017 / 17:43

1 answer

1

If you want to call the stateChanged function with data "preconfigured podes usar o .bind", so the function is not invoked (which is what is happening now) and you can use that data later. >

xmlHttp.onreadystatechange = stateChanged.bind(null, DADOS,TAG);

and then use as you have.

But this has limitations, since you're using a xmlHttp global ...

I suggest changing the logic a bit and doing something like this:

function Geral(PAGINA, DADOS, TAG, done) {
  var xmlHttp = GetXmlHttpObject();
  var url = PAGINA;
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) done(DADOS, TAG, xmlHttp.responseText)
  };
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

Geral("pagina.asp?a=1", "dados", dados, function(A, B, res) {
  document.getElementById(A).innerHTML = res;
  newTag(B);
})
    
26.10.2017 / 17:55