title: Write JavaScript variable on HTML Body
body: <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
      &lt;head&gt;
          &lt;meta name="viewport" content="initial-scale=1.0, user-scalable=no" /&gt;
          &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8" /&gt;
          &lt;title&gt;Google Maps JavaScript API v3 Example: Reverse Geocoding&lt;/title&gt;
          &lt;link
          href="https://developers.google.com/maps/documentation/javascript/examples/default.css"
          rel="stylesheet" type="text/css" /&gt;
          &lt;script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              var geocoder;
  
              var variablejs;
  
              geocoder = new google.maps.Geocoder();
  
              var input = "41.021355,-96.020508";
              var latlngStr = input.split(",", 2);
              var lat = parseFloat(latlngStr[0]);
              var lng = parseFloat(latlngStr[1]);
              var latlng = new google.maps.LatLng(lat, lng);
  
              geocoder.geocode({
                  'latLng': latlng
              }, function (results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {
                      if (results[0]) {
                          variablejs = results[0].formatted_address; * * //document.write(variablejs); Not working**
                          *
                          //window.location.href = "http://localhost/test2.php?ad="+ variablejs; Working*
                      } else {
                          alert("No results found");
                      }
                  } else {
                      alert("Geocoder failed due to: " + status);
                  }
              }); * * document.write(variablejs); * * //writes undefined variable (not working)
          &lt;/script&gt;
      &lt;/head&gt;
  
      &lt;body&gt;&lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>The section (<strong>//document.write(variablejs); Not working</strong>), won't write the variable "variablejs", but when I use the method (<strong>//window.location.href = "http://localhost/test2.php?ad="+ variablejs; Working</strong>), it will write the url and forward the page passing the variable as a parameter. What I want to do is print that variable in the body of the same HTML. 
  I tried to initialize first the variable then write into the Body but It looks like that section is not accessible like a global variable or something like that. Please help. </p>