I want to know how I can wait for one script to load before loading the next one. For example(and also actually load another page into the page completely):
<html> <script src = "script1"></script> <script src = "script2"></script> <script> $(document).ready( function() { $("#content1").load("index2.html"); }); </script> </html> So after loading script1 properly(and run all thats in there), I want to load script 2, and after that is loaded completelt, I want to load the document index2.html into this html-document.
What I have been trying so far is this but it is not working exactly as I want to.
File1: scriptOrderTest.html
<html> <div id ="content1"></div> <script src="js/jquery-3.2.1.min.js"></script> <script> var variable1 = undefined; $(document).ready( function() { $.ajax({ url: "script1.js", dataType: "script", success: () => { $.ajax({ url: "script2.js", dataType: "script", success: () =>{ // do the stuff you need $("#content1").load("index2.html"); } }); } }); }); </script> </html> File2: script1.js
console.log("Starting script 1"); function revealTime(){ console.log("Oh, I just wasted 5 seconds of my life") } //This script is just wasting time setTimeout(revealTime,5000); File3: script2.js
console.log("Starting script 2"); function revealTime(){ console.log("Oh, I just wasted 5 seconds of my life AGAIN") ; variable1 = "Pizza"; } //This script is just wasting time setTimeout(revealTime,5000); File4: index2.html
<html> <script src="js/jquery-3.2.1.min.js"></script> <script> document.write("The variable is:"+variable1); </script> </html> Firstly, So the goal is to wait for the scripts to finish so that the variable can get a value before writing it on the page. This is not the case with the code above.