Wednesday, 16 June 2021

JS scripts not downloaded and evaluated sequentially

I have 3 <script> tags as the last elements in <body>. They load external .js scripts:

    ...
  </div>
  <script src="https://somewhere.com/scripts/script1.js"></script>
  <script src="https://somewhere.com/scripts/script2.js"></script>
  <script src="https://somewhere.com/scripts/script3.js"></script>
</body>
</html>

All scripts contain only one line: console.log(Start script X), where X is the script number. I was expecting for the scripts to be evaluated sequentially and the output to be:

Start script 1
Start script 2
Start script 3

But the output seems random. To be more precise, it looks like the order of evaluation depends solely on the order in which the scripts finish downloading.

Was I right to expect evaluation order to mimic the order they were referenced in HTML? If not, how do I make sure that they are evaluated in the correct order (besides merging them all in one .js file)?



from JS scripts not downloaded and evaluated sequentially

No comments:

Post a Comment