Aaron
Aaron

Reputation: 839

Does $.ready fire before or after all inline <script>'s have been loaded?

At the core, this is a javascript question but I'm interfacing the event with jQuery.

Here's an example case:

<html>
  <head>
    <script src="waiting_on_ready.js" />
  </head>
  <body>
    <script src="http://somewhere.com/not_cached.js"></script>
  </body>
</html>

Does the $.ready callback in waiting_on_ready.js have to wait for not_cached.js to be loaded? Does it wait for not_cached.js to be executed?

Upvotes: 7

Views: 2826

Answers (4)

Cloxure
Cloxure

Reputation: 1861

the $.ready script executes after the page is fully loaded, as the onload event does, however if an inline script is found then it is executed.

Upvotes: 0

McKayla
McKayla

Reputation: 6949

Ready fires when all DOM elements are ready for manipulation.

When the browser comes across a script element, DOM parsing stops until the script has been executed, so because of how it all works, it fires after all scripts are loaded.

Upvotes: 8

Demian Brecht
Demian Brecht

Reputation: 21368

$(document).ready() would not be triggered prior to not_cached.js to be loaded and executed (assuming execution in not_chached.js is synchronous)

Upvotes: 1

user166390
user166390

Reputation:

Yes and yes.

The execution of <script> elements is synchronous except when using the HTML5 "async" attribute. The synchronous execution of JavaScript is required (unless otherwise requested not to be) because the JavaScript can modify the document stream through document.write, etc. (However, the actual fetching of resources may be in parallel.)

Happy coding.

Upvotes: 10

Related Questions