Error injecting source script from googlemaps via method


I would like geocomplete not to get this error, but to work, it depends on the API google maps. I removed the following script from the header and passed via method:

 <script id="id_script" src=""></script>//(1)códigoremovido


 <script src="/js/jquery.geocomplete.min.js" type="text/javascript"></script>
 //1. antes ele estava aqui ----
  <script id="id_script" src="default.js"></script>

The method I created does the same as the script (1) that was loaded in head:

function googleMapsScriptById(client, id) {
    var url_maps = ''+client+'v=3.21&language=pt-br&libraries=places&components=country:Brasil';

    (function () {
        var gmaps = document.createElement('script');
        gmaps.type = 'text/javascript';
        gmaps.async = true;
        gmaps.src = url_maps;
        var ref = document.getElementById(id);
        ref.parentNode.insertBefore(gmaps, ref);


But now I'm getting the following error message from the geocomplete that did not have this:


jquery.geocomplete.min.js: 8 Uncaught ReferenceError: google is not defined

What can be, is there any way to adjust the sync between the two?

My conclusions:

Even though I reverse, the loading order:

<script id="antes_desse"></script>
  <script src=""id="jquery"></script>
 <script src="/js/jquery.geocomplete.min.js" type="text/javascript"></script>
  <script id="id_script" src="default.js"></script>



The error still persists, because I understand that the libraries in the head are preloaded before executing the method: googleMapsScriptById(); , and in this case, as the others are already in head, and this is google, still has not been loaded, its dependencies simply display error.

I wonder if there is any way to do this, how do we do with Promise?

asked by anonymous 21.10.2016 / 16:46

1 answer



The Google Maps Javascript API is being loaded before the Geocomplete API, where it needs the Google API.

In the role you created to enter the Google Maps API, enter Geocomplete later.

UPDATE : The " async " attribute will not block loading of other scripts, if you remove it, the load may be slower, but the Google API Maps is already loaded.

More information here: Google Maps Javascript API

24.10.2016 / 15:01