I'm trying to add scripts to HTML files dynamically using JavaScript (to save space - I've many HTML files each of which contains many scripts, so they would al be added simply by including one script file).
The question Append javascript to HTML fields using through javascript is not really what I was thinking about...
However, in the script file that adds scripts and other resources to the HTML file in question, the scripts do add the external files, but the scripts (those inserted dynamically) are not loaded.
Directory Structure
root
├ index.html
├ universal-scripts.js (adds scripts to .html files dynamically)
├ styling.css (included into .html files)
└ app-scripts
└ others.js (problem file)
universal-scripts.js
...
var others = document.createElement("script");
others.setAttribute("src", "app-scripts/others.js");
...
document.getElementsByTagName("head")[0].innerHTML = document.getElementsByTagName("head")[0].innerHTML + others.outerHTML;
The script is inserting the element but functions from others.js
are not usable from within index.html
.
Any help would be much appreciated. Thanks!
I'm using Electron if that means anything...