title: Load Script and Execute only if needed
body: <p>I need to find if there are any divs with class="map" in a page <a href="http://codepen.io/mdmoura/pen/jBrfd" rel="nofollow">Code Pen Example</a>.</p>
  
  <p>If there are, and only in that case, load Google Maps API.</p>
  
  <p>Then use it to load the maps into the divs using data attributes for lat and long values.</p>
  
  <p>So I have the following:</p>
  
  <pre><code>&lt;div class="map" data-long="51.5072" data-lat="0.1275"&gt;
    Replace by map 1
  &lt;/div&gt;
  
  &lt;div class="map" data-long="74.0059" data-lat="40.7127"&gt;
    Replace by map 2
  &lt;/div&gt;
  </code></pre>
  
  <p>I was considering using something like (this should be loaded only if map divs exist):</p>
  
  <pre><code>$.getScript('https://www.google.com/jsapi', function()
  {
    google.load('maps', '3', { other_params: 'sensor=false', callback: function()
    {
  
  
    }});
  });
  </code></pre>
  
  <p>The code I would like to apply to each map is something like <a href="https://developers.google.com/maps/tutorials/fundamentals/adding-a-google-map#the_finished_code" rel="nofollow">Google Maps Code</a>:</p>
  
  <pre><code>  function initialize() {
      var map_canvas = document.getElementById('map_canvas');
      var map_options = {
        center: new google.maps.LatLng(44.5403, -78.5463),
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var map = new google.maps.Map(map_canvas, map_options)
    }
    google.maps.event.addDomListener(window, 'load', initialize);
  &lt;/script&gt;
  </code></pre>
  
  <p>Thank You,
  Miguel</p>