I want to test google api. I created three files at once: index1.html , main.css , main.js

I connect everything at once into one html (fragment)

 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> <script> var map; function initialize() { map = new google.maps.Map(document.getElementById('map-canvas'), { zoom: 8, center: {lat: -34.397, lng: 150.644} }); } google.maps.event.addDomListener(window, 'load', initialize); </script> 

After I want to spread over the files. js I drop into the main.js file (I delete js in html and connect the link in place of the code). I'm testing - everything works.

Then I copy api and put a new file googl-api.js file, everything works (below is the structure)

 <script src="googl-api.js"></script> <script src="main.js"></script> 

And now hocus pocus - I cut out the contents of the main.js file and insert googl-api.js at the end of the file and it does not work! What nonsense? I basically like what works in different files, but I can't figure out what's the catch? It seems so and so should work!


UPD

Error that is displayed in the browser console:

 Uncaught TypeError: Cannot read property 'addDomListener' of undefined 

    2 answers 2

    The reason of inoperability of the collected js in the file https://maps.googleapis.com/maps/api/js?v=3.exp . It internally loads the google api functionality (file https://maps.gstatic.com/maps-api-v3/api/js/21/7/intl/ru_ALL/main.js ) with a simple command

     document.write('<' + 'script src="' + src + '"><' + '/script>'); 

    And your code is executed earlier, that is, when the functionality is not yet loaded.

      Use <script src="https://maps.googleapis.com/maps/api/js?v=3.exp?callback=initialize"></script>

      More information is available here: https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API

      • Well, the question was slightly different: why can not I put everything in one file? (it is not advisable to use additional links for me); And separately it works - there is no question. And in the "heap" does not work (( - Igor Kalamurda