Free from the shackles of the browser (4)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.