// Create the script tag, set the appropriate attributes var script = document.createElement('script'); script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyBWesZdBxC0wGJIXI6z_9tVRfSti6jcVQE&callback=initMap'; script.defer = true; script.async = true; // Attach your callback function to the `window` object window.initMap = function() { // JS API is loaded and available }; // Append the 'script' element to 'head' document.head.appendChild(script);