I just realized that I lack the fundamental knowledge of what exactly happens as a page is being loaded into a browser.
Assume I have a structure like this:
<head>
<script src="jquery.js" type="text/javascript"></script>
<script src="first.js" type="text/javascript"></script>
</head>
<body>
...
<script type="text/javascript" id="middle">
    // some more JS here...
</script>
...
<script src="last.js" type="text/javascript"></script>
</body>
Here are the questions I have:
What sequence are things happening in? First the DOM then the JS is executed, is it vice-versa, or is it simultaneous (or as soon as the JS files finish downloading, without any regard to the DOM)? I know that scripts are loaded in order.
Where does
$(document).ready()fit in? In Firebug's Net tab I seeDOMContentLoadedevent and theloadevent. Is$(document).ready()triggered when theDOMContentLoadedevent fires? Couldn't find any concrete info on this (everyone merely mentions "when the DOM is loaded").What exactly does "when the DOM is loaded" mean? That all HTML/JS has been downloaded and parsed by the browser? Or just the HTML?
Is the following scenario possible: there is a
$(document).ready()which calls code inlast.js, but runs before last.js has loaded? Where would it most likely be (infirst.jsor the inline code block)? How can I prevent this scenario?
I want to undestand the big picture of what happens when and what depends on what (if at all).