0

I know that from the main HTML file, I can refer to my own external JS file with <script src="/path/to/js.js">. However, I'm using google maps in that external file, which I would normally load with <script src="https://maps.googleapis.com/maps/api/js"></script>, without an external file. Does it matter whether the script src tag is in the main or external file? How can I insert a reference to google's JS file into my external JS file?

Forager
  • 307
  • 2
  • 10
  • If the script tag isn't in the main file, how would it know where to find the external file? – Scott Hunter Jun 22 '15 at 20:23
  • I would have a script tag in the main file pointing to my own 'external JS' file that I wrote. I want my external JS file to know where Google Maps' JS file is. – Forager Jun 22 '15 at 20:25

1 Answers1

0

To load an external javascript file with javascript, you could do something like this:

var script = document.createElement('script');
script.src = 'URL_TO_EXTERNAL_JS';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);

This snippet creates a new script tag, sets the src and async attributes and appends it to the documents head.

Alex
  • 2,398
  • 1
  • 16
  • 30