Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138249

Can I simplify this script that simply adds a script to the DOM?

$
0
0

I used this as a starting point, but realize it depend upon a div being present.

Curious as to wether or not I can just use appendChild to append the script directly to document.body?

// loads a script using the DOM and an optional argument to bust the cache
function loadScript(path, bust) {
  console.log("Loading | " + path + " | " + bust)
  const ref = document.getElementsByTagName( 'div' )[ 0 ];
  const script = document.createElement( 'script' );
  if(bust){
    path = path + '?cache_buster=' + bust;
  }
  script.src = path;
  ref.parentNode.insertBefore( script, ref );
}

Viewing all articles
Browse latest Browse all 138249

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>