Say if I want to execute some code once the page is loaded. Is it more recommended to use the load
event listener, like this:
window.addEventListener("load", function() {
// example code
for (var x = 0; x < l; x++) {
console.log("yo");
}
});
or can I just write what I want outside of any scope:
// example code
for (var x = 0; x < l; x++) {
console.log("yo");
}
I mean, either one is gonna get executed once the page loads right? Does it have to do with, in the second case, wanting to access elements that haven't yet been loaded when the script does?
Thanks in advance.