How can I insert a script into HTML head dynamically using JavaScript? [duplicate]

var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type="text/javascript";
script.onload = function() {
    callFunctionFromScript();
}
script.src="https://stackoverflow.com/questions/5132488/path/to/your-script.js";
head.appendChild(script);

Leave a Comment