Reputation: 588
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...
Upvotes: 2
Views: 4225
Reputation: 1520
Probably the best way is to add the reference to the file in your package.js file since this is where all of the rest of your projects source file are referenced. Why load it latter when you can load it in your core.
Here is a Electron.js tutorial that talks about the package.js file.
Creating Your First Desktop App With HTML, JS and Electron]1
Here is a package.js interactive guide. package.js walk through
Upvotes: 2