I have this index.html:

<!DOCTYPE html>
    <script src="src/index.js"></script>

and this index.js:


How come alert('2') is apearing before alert('1').
As far as I know, loading src/index.js should be a blocking operation.



2 Answers

Codezters On

You could use the SetTimeout() function to make the second alert begin, or, call a function in the other script when the alert is exited, using:

// You could either put all of the other script in a function and call that here, or change the src of the script
document.GetElementById("ScriptID").src = "src/index.js";

Hope I could help!

elena a On

Inserting a script element into the document can be accompanied by an onload callback function to ensure a sequential process. See Mozilla published docs under Web/API/HTMLScriptElement There are explanations and examples there.