What is the best practice for loading an external .js file into an .html file? (no inline calls e.g., <body onload="...)
I have a basic HTML page where I’d like to load an external .js file. When I have the code in a script tag at the bottom of my HTML page everything works as it should.
However if I cut the js code and paste it into an external .js file and call the file from either the bottom of the <head> tag or place the call to the script just above the closing </body> tag the script doesn’t run.
Unsuccessful, but I’ve also tried wrapping the code in the external .js file in:
function init() {
// Put your code here
}
window.onload = init;
This will make the Javascript code accessible from other external javascript files, as well as inline Javascript.