Answers for "Dynamically load external JavaScript CDN"

1

Dynamically load external JavaScript CDN

const loadGoogleMaps = (callback) => {
  const existingScript = document.getElementById('googleMaps');

  if (!existingScript) {
    const script = document.createElement('script');
    script.src = 'https://maps.googleapis.com/maps/api/js?key=<API Key>&libraries=places';
    script.id = 'googleMaps';
    document.body.appendChild(script);

    script.onload = () => {
      if (callback) callback();
    };
  }

  if (existingScript && callback) callback();
};
Posted by: Guest on September-30-2021

Code answers related to "Dynamically load external JavaScript CDN"

Code answers related to "Javascript"

Browse Popular Code Answers by Language