【发布时间】:2020-06-24 23:13:12
【问题描述】:
在How, When, And Why Script Loaders Are Appropriate的文章上,我发现以下评论没有明确解释:
// Very simple asynchronous script-loader
// Create a new script element
var script = document.createElement('script');
// Find an existing script element on the page (usually the one this code is in)
var firstScript = document.getElementsByTagName('script')[0];
// Set the location of the script
script.src = "http://example.com/myscript.min.js";
// Inject with insertBefore to avoid appendChild errors
firstScript.parentNode.insertBefore( script, firstScript );
他们通过使用insertBefore 而不是appendChild 避免了哪些错误?
为什么appendChild 会导致任何错误?
【问题讨论】: