title: Accessing javascript variable in php and prevent body to load before some event
body: <p>I am creating one application where the location of the user will be fetched, and it will be shows in the  map. Also, The city of the user I am fetching. I have created one separate master.js where I have putted all the fetching code. Now I have put function in window.onload which will call the function of map where latitude and longitude will be fetched and one function will be called from there which is to fetch the city of the user.</p>
  
  <pre><code>function showcityname(userLatlng){
      var geocoder = new google.maps.Geocoder();
      geocoder.geocode({
        "location": userLatlng
      },
      function(results, status) {
        if (status == google.maps.GeocoderStatus.OK)
          var city_name = results[0].address_components[4].long_name;//to get address
          //window.location="?city_name="+ city_name;
  
          alert(city_name);
           document.cookie = "name=" + city_name ;        
          //fetchcity(city_name);
          // window.location = "?city=" + city_name;      
  
       // else
        //  document.getElementById("error").innerHTML += "Unable to retrieve your address" + "&lt;br /&gt;";
     });
  }
  </code></pre>
  
  <p>Now I want to make application like using this city_name the query will be fired to database, and it will be shown on the page the relative data from the database. The problem is the body loaded before it fetches the city. So the relevant data is not shown to page. I want to make that the city_name variable first should be assign to variable and then the body should execute so I can show relative data. I have user document.cookie but it did not work if user first time comes to my website, It will worked only if the user refresh the page. May be its problem of window.onload.. How to stop the body to load before the city name comes.Thanks in advance.</p>