4

I ran into an issue today where my script with type="module" is executed before my main.js script which holds some important functions. Or it has not yet finished loading the first script before it loads the second.

Then my module is complaining that a function is not defined which was defined in my main.js. This doesn't happen every time though. Other times my main.js gets loaded first and everything works the way it should.

<script src="http://localhost/newblog/js/main.js"></script>
<script type="module" src="http://localhost/newblog/js/blog.js"></script>

Is there a way to make the second script wait until my first script has finished loading and only then load the second script?

Liga
  • 3,291
  • 5
  • 34
  • 59
  • 1
    `module` scripts are deferred by default. So in your case the second script should run after the first script. Try logging in both script to see which script is running first. – Prakash Sharma Feb 23 '19 at 07:58
  • See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#Notes – Prakash Sharma Feb 23 '19 at 07:59
  • Does "main.js" include asynchronous processes? Can you create a stacksnippets or plnkr https://plnkr.co to demonstrate? See https://stackoverflow.com/help/mcve – guest271314 Feb 23 '19 at 18:47
  • Fairly complete answers to this question, I believe, can be found here: https://stackoverflow.com/questions/403967/how-may-i-reference-the-script-tag-that-loaded-the-currently-executing-script – AlexJeffcott Feb 16 '22 at 13:16

0 Answers0