dynamically load external javascript cdn

Solutions on MaxInterview for dynamically load external javascript cdn by the best coders in the world

showing results for - "dynamically load external javascript cdn"
Maddy
12 Mar 2016
1const loadGoogleMaps = (callback) => {
2  const existingScript = document.getElementById('googleMaps');
3
4  if (!existingScript) {
5    const script = document.createElement('script');
6    script.src = 'https://maps.googleapis.com/maps/api/js?key=<API Key>&libraries=places';
7    script.id = 'googleMaps';
8    document.body.appendChild(script);
9
10    script.onload = () => {
11      if (callback) callback();
12    };
13  }
14
15  if (existingScript && callback) callback();
16};