<head>
<script src="1.js"></script>
</head>
<body>
<script src="3.js"></script>
</body>
//js1.js
let head = document.querySelector('head');
let script = document.createElement('script');
script.src = '../js/js2.js';
script.async = false;
head.append(script);
console.log(1);
//js2.js
console.log(2);
//js3.js
console.log(3);
The order I got in my browser was: 1, 3, 2.
I expected 1, 2, 3 because 1.js
puts the script that executes 2.js
immediately below it and 3.js
only executes at the end of body. I was under the impression i) in dynamic scripts, loading starts as soon as the script is appended to the DOM (and presumably execution will follow).
Is the reason why js.3
gets executed last got something to do with a rule that says dynamic script tags aren't executed during the 'initial parsing of the html
'?