I have a modular single page html that dynamically loads content html based on a wide variety of attributes which are not important. The code below works great except the script is not loaded. I have a place in the dynamic load function to insert script but can't find any examples of this type of loading. Everything assumes an src file however I know you can do something like below with element.text but not sure how to load that from the html text.
Note that all the references I have found load via an .src and not the .text which I need.
function dynamicLoad(elem, sourceCode) {
// load html not the issue
elem.innerHTML = sourceCode;
// load JS -- this is with solution ---
var doc = document.implementation.createHTMLDocument(); // Sandbox
doc.body.innerHTML = sourceCode; // Parse HTML properly
// --- This is the solution below, will load all scripts found
[].map.call(doc.getElementsByTagName('script'), function(el) {
var e = document.createElement('script');
e.text = el.textContent;
document.body.appendChild(e);
}
}