0

I have a script (Google Maps, to be exact) that is loading dynamically, and for a couple of reasons, I cannot alter how this initially loads by default.

Through JavaScript, I would like to find the string libraries=places and change it to libraries=places,visualization after the fact

So, the originally output:

<script type="text/javascript" 
          src="https://maps.googleapis.com/maps/api/js?libraries=places">
</script>

Would be:

<script type="text/javascript" 
          src="https://maps.googleapis.com/maps/api/js?libraries=places,visualization">
</script>

Is this even possible?

Thanks In Advance

Ryan Dorn
  • 417
  • 7
  • 20

2 Answers2

0

this solution should apply to yours as well: How do I include a JavaScript file in another JavaScript file?

script.src = url; will be the line where you add the src url however you like

Community
  • 1
  • 1
Sam
  • 23
  • 4
  • This requires him to change the original HTML to remove the ` – Barmar May 18 '15 at 22:14
-1

First of all Give the google map script tag an ID:

<script id="google_map_script" type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>

Then do:

var google_map_script = document.getElementById("google_map_script")
google_map_script.setAttribute("src", "https://maps.googleapis.com/maps/api/js?libraries=places,visualization")

Edit: since apparently this isn't what is needed then what about this:

var script = document.createElement('script');

my_awesome_script.setAttribute('src','https://maps.googleapis.com/maps/api/js?libraries=places,visualization');

document.head.appendChild(script);

Also have a look into using postscribe.

Thermatix
  • 2,757
  • 21
  • 51
  • wouldn't that load the un-changed tag url first, then load the 2nd one (if it did anything)? – dandavis May 18 '15 at 22:00
  • @dandavis I don't think you can do any better. Javascript can't alter anything until it has been loaded into the DOM, and loading a ` – Barmar May 18 '15 at 22:12
  • @Barmar: i didn't see the part about not changing the script tag. to be 100% accurate, you can use `document.write("", which can snuff yet-unseen scripts, but there's likely an easier way, and if he's "going in" after the tag, it's too late. – dandavis May 18 '15 at 22:18