Why doesn this JavaScript code work without window.onload?
JavaScript is executed in a top-to-bottom order along with the HTML (If the script is external or inline does not matter.) So if you load an external script, the page won’t finish building until that script has been executed. This means that the element you’re referencing (id “foo”) is not accessible until the HTML for it has been loaded and handled. If your