title: How can I dynamically add the Google Maps API via javascript?
body: <p>I am trying to use the Google Maps API on a page which I can only edit through an external javascript file. The problem is, when I try to load the Google Maps API dynamically using getScript, it doesn't load. </p>
  
  <p>Is there any way to load the Google Maps API dynamically?</p>
  
  <p><strong>This works:</strong></p>
  
  <p><a href="http://jsfiddle.net/2XVKL/2/" rel="noreferrer">http://jsfiddle.net/2XVKL/2/</a></p>
  
  <pre><code>&lt;script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  function codeAddress() {
  
      new google.maps.Geocoder().geocode({'address':document.getElementById('address').value}, function(results, status) {
  
          if (status == google.maps.GeocoderStatus.OK) {
  
              alert('worked!');
  
          } 
  
      });
  
  }
  &lt;/script&gt;
  
  &lt;body&gt;
      &lt;input id="address" type="textbox" value="Sydney, NSW"&gt;
      &lt;input type="button" value="Submit" onclick="codeAddress()"&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p><strong>But I need it the api to be dynamically loaded using only javascript, like this:</strong></p>
  
  <p><a href="http://jsfiddle.net/2XVKL/3/" rel="noreferrer">http://jsfiddle.net/2XVKL/3/</a></p>
  
  <pre><code>&lt;script&gt;
  $("document").ready(function () {
  
      $.getScript("https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false");
  
  });
  
  
  function codeAddress() {
  
      new google.maps.Geocoder().geocode({'address':document.getElementById('address').value}, function(results, status) {
  
          if (status == google.maps.GeocoderStatus.OK) {
  
              alert('worked!');
  
          } 
  
      });
  
  }
  &lt;/script&gt;
  
  &lt;body&gt;
      &lt;input id="address" type="textbox" value="Sydney, NSW"&gt;
      &lt;input type="button" value="Submit" onclick="codeAddress()"&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p><strong>Edit: found the solution here - <a href="https://gist.github.com/jorgepedret/2432506" rel="noreferrer">https://gist.github.com/jorgepedret/2432506</a></strong></p>