We already know that parallel downloads of script files can increase the load speed of a page. But there is an urgent need to solve the problem, that is, for Firefox browser optimization. The optimization approach we used before, whether it was a simple, practical document.write or a tasteless defer attribute, was ignored by the Firefox browser. However, Firefox is not absolutely "stubborn", developers still have a way to optimize it.
This method is to dynamically add a SCRIPT element.
Dynamically adding a SCRIPT element
I don't know if the term "dynamic Add script element" is correct, I mean using JavaScript programming to add script elements to
Dynamically adding a SCRIPT element <title>Untitled Page</title>
<script type="text/javascript" language="javascript">
for (var i = 0; i < 5; i ++)
{
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "Script.ashx?a=" + i;
document.getElementById('head').appendChild(script);
}
</script>
<body>
...
</body>
Note that because the page is not loaded when JavaScript code executes, So it's not possible to use the document.getElementsByTagName method to get the head element, we can only add an ID to the head element and use the document.getElementById method to get it. When you open this page, you will find that both IE (Figure 9) and Firefox (Figure 10) Element loading will find the effect of optimization:
Dynamic load script element effect in Figure 9:ie
Dynamic load script element effect in Figure 10:firefox