Yes, that's right. If the script you are importing needs to access DOM elements, it will need to be imported after the DOM elements are ready.
However, you can import the script in the HEAD
if you want to but the script you are importing will need to have the following to listen for the DOMContentLoaded
event:
Javascript:
document.addEventListener("DOMContentLoaded", function(event) {
// your code to run - the DOM is ready!
});
jQuery:
$(document).ready(function() {
// your code to run - the DOM is ready!
});
Or you can simply import the script at the bottom of the BODY
like you have found out, and the DOM will be ready by the time the script is requested.
The DOM being ready does not necessarily mean any images are fully loaded. If you want to wait until all images, scripts and DOM are ready, you will need to listen for window's onLoad
event to be fired, and that can be achieved by doing the following:
Javascript:
window.onload = function(e) {
// do something when everything is ready
}
jQuery:
$(window).on('load', function(e) {
// do something when everything is ready
});