create script tag dynamically javascript code example

Example 1: dynamically add script code to page

var newScript = document.createElement("script");
newScript.src = "http://www.example.com/my-script.js";
target.appendChild(newScript);

Example 2: javascript create script tag

var script = document.createElement("script");
script.src = "https://url.com/";
document.head.appendChild(script);

Example 3: dynamically add script code to page

var newScript = document.createElement("script");
var inlineScript = document.createTextNode("alert('Hello World!');");
newScript.appendChild(inlineScript); 
target.appendChild(newScript);