Javascript passed to jQuery.ready() will run after the DOM is fully loaded but doesn’t wait for all assets such as images to be received (unlike jQuery.load()). Cool. But will javascript placed in <script> tags in the <head> of a document always run before code passed to jQuery.ready()?
I’m assuming the answer is yes, but couldn’t find anything specifically clarifying this.
Scripts in script tags run in sequential order in the order they are encountered in the HTML file. So, a script in the HEAD tag runs as soon as the HEAD section of the document is loaded and no scripts after that run until that script has finished executing.
Scripts containing
$(document).ready(fn)will schedule their callback to run later after the whole document has been loaded.So, since a script in the HEAD section is long BEFORE the body of the document has loaded, scripts in the HEAD section will run before a callback specified using
$(document).ready()is run.