load and execute order of scripts

后端 未结 4 1956
我在风中等你
我在风中等你 2020-11-21 11:34

There are so many different ways to include JavaScript in a html page. I know about the following options:

  • inline code or loaded from external URI
  • inc
4条回答
  •  孤街浪徒
    2020-11-21 12:10

    After testing many options I've found that the following simple solution is loading the dynamically loaded scripts in the order in which they are added in all modern browsers

    loadScripts(sources) {
        sources.forEach(src => {
            var script = document.createElement('script');
            script.src = src;
            script.async = false; //<-- the important part
            document.body.appendChild( script ); //<-- make sure to append to body instead of head 
        });
    }
    
    loadScripts(['/scr/script1.js','src/script2.js'])
    

提交回复
热议问题