0

I am new to javascript and wrote the following script to send an HTTP request to my webserver and receive back the response. I defined a variable 'flag' that I will use later in the script. However, its value is not getting changed in the script even after receiving the response.

var flag=0;
loadDoc();
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "tt.html", true);
  xhttp.send();
  xhttp.onreadystatechange=test;
  function test() {
    if (this.readyState == 4 && this.status == 200) {
      document.body.innerHTML = this.responseText;
      flag=1;
      console.log(flag);     // outputs 1
    }
  }
  console.log(flag);     // outputs 0 (It should have returned '1')
}

0 Answers0