Any way to control Javascript Async Load Order?

后端 未结 2 1303
名媛妹妹
名媛妹妹 2020-12-19 00:58

How do I set the load and execution order of two external async Javascript files?

Given the following...



        
相关标签:
2条回答
  • 2020-12-19 01:40

    If you need a solution for IE9 (since it does not support the defer attribute), I have created a small loader script:

    https://github.com/mudroljub/js-async-loader

    It loads all your scripts asynchronously and then executes them in order.

    0 讨论(0)
  • 2020-12-19 01:43

    You want to use defer if you want to preserve the execution order. What defer does is it async downloads the script, but defers execution till html parsing is done.

    <script src="framework.js" defer></script>
    <script src="scripts.js" defer></script>
    

    However, you may want to start creating custom bundles once the number of scripts go higher.

    You can see the difference here

    0 讨论(0)
提交回复
热议问题