1
我正在動態創建腳本標記並將async
屬性設置爲true
,它正確顯示在dom中,但查看網絡選項卡會顯示不同的故事。爲什麼在加載的初始文件和腳本標籤之間存在如此大的差距以便動態加載?瀏覽器腳本標記「異步」屬性不起作用
DOM
Network標籤
的index.html
<head>
<meta charset="UTF-8">
<title>React App</title>
<script>
function loadjscssfile(BASE, filename, filetype) {
if (filetype == "js"){ //if filename is a external JavaScript file
filename = BASE + jsManifest[filename];
var fileref = document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
fileref.setAttribute("async", "true")
}
else if (filetype == "css"){ //if filename is an external CSS file
filename = BASE + cssManifest[filename];
var fileref = document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref != "undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
</script>
<!-- dynamically load hashed CSS files -->
<script src="./css-manifest.js"></script>
<script src="./js-manifest.js"></script>
<script>
var BASE = '../stylesheets/';
for (var key in cssManifest) {
loadjscssfile(BASE, key, 'css');
}
</script>
</head>
<body>
<div id="app"></div>
<!-- dynamically load hashed JS files -->
<script>
var BASE = '../prod/';
for (var key in jsManifest) {
loadjscssfile(BASE, key, 'js');
}
</script>
</html>
太棒了!感謝澄清所有這些爲我:) – Clement