Dynamic script addition should be ordered?

后端 未结 6 2009
花落未央
花落未央 2020-12-16 04:31

I\'m adding some

6条回答
  •  一整个雨季
    2020-12-16 05:27

    No, you cannot expect that all browsers will defer execution of both scripts until both are loaded (**especially when you are adding them dynamically).

    If you want to execute code in B.js only after A.js is loaded then your best bet is to add an onload callback to A.js that sets a variable and another one to B.js that checks to see if that variable has been set, then it executes the necessary function in B.js if it has (and if A.js has not loaded, it starts a timer that periodically checks until it has loaded).

提交回复
热议问题