Delay script loading

后端 未结 7 704

So if I have the following:


and I simply want

7条回答
  •  遥遥无期
    2020-12-03 07:31

    Mozilla Developer Network explains various approaches:

    MDN Async Script Techniques

    
    

    or

    var script = document.createElement('script');
    script.src = "file.js";
    document.body.appendChild(script);
    

    or if your JavaScript is in a String:

    var blob = new Blob([codeString]);
    var script = document.createElement('script');
    var url = URL.createObjectURL(blob);
    script.onload = script.onerror = function() { URL.revokeObjectURL(url); };
    script.src = url;
    document.body.appendChild(script);
    

    There is also good information when async is not async as well as how to get around those cases.

提交回复
热议问题