thesonix
thesonix

Reputation: 3260

JavaScript execution

I have two scripts, which are included in the HTML body. In the first script I'm initializing a JS-Object which is referenced in the second script tag.

<body>
  ...
  <script type="text/javascript" src="http://url/script.js"></script>
  <script type="text/javascript">
     obj.a = 1000;
     obj.do();
  </script>
</body>

What is happening, if loading time of the first script (via HTTP) is slow? How is the execution order of the JS in the body?

Upvotes: 0

Views: 80

Answers (1)

SLaks
SLaks

Reputation: 887225

<script> tags within page source are executed synchronously together with the page load.

The browser will not parse or render any HTML after the <script> tag until the script finishes downloading and executing.

This is why it's better to move all <script> tags to the bottom of the page, so that the HTML is rendered first.

Upvotes: 2

Related Questions