shabashj
9/4/2016 - 7:41 AM

Insert <script> to Html DOM

Insert

(function (d, script) {
    script = d.createElement('script');
    script.type = 'text/javascript';
    script.async = true;
    script.onload = function(){
    // remote script has loaded
    };
    script.src = 'http://localhost:8080/myScript.js';
    d.getElementsByTagName('head')[0].appendChild(script);
    }(document));


var scr = document.createElement("script");
scr.setAttribute("type", "text/javascript");
scr.setAttribute("src", src);
document.getElementById("someId").appendChild(scr);

// or jquery
var scr = document.createElement("script");
scr.setAttribute("type", "text/javascript");
scr.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js");
document.getElementsByTagName("body")[0].appendChild(scr);