Set variables to call a function

Asked

Viewed 41 times

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 it like this, but it didn’t 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 is wrong?

1 answer

1


If you want to call the job stateChanged with "pre-configured datapodes usar o.bind`. So the function is not invoked (which is what is happening now) and you can use this data later.

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

and then use as you have.

But this has limitations, since you’re wearing one xmlHttp global...

I suggest you change your logic a little bit and do 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);
})

  • thank you very much, attended to my needs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.