add script tag with js code example

Example 1: include script in html

<script type="text/javascript" src="/path/to/script.js"></script>

Example 2: dynamically add script code to page

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

Example 3: how to link javascript to html

<script src = "index.js"></script>

Example 4: dynamically add script code to page

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