You've got your website all set up but are experiencing slow load times for some customers, especially on slower connections. One possible cause is inefficient network usage because of serialized loading of external Javascript files.

Static links in <head>

If you include script files in the HEAD element both IE and Firefox will download the scripts serially, waiting for each one to finish downloading before proceeding with the next one. Take the following HTML snippet that loads two external javascripts:



    <script type="text/javascript" src="Script1.js"></script>

    <script type="text/javascript" src="Script2.js"></script>


Network traffic for loading scripts in <head> tag

In this common scenario a large amount of network bandwidth can potentially go unused.

Loading the scripts in parallel

You have two options for loading scripts in parallel:
  • Attach them as elements dynamically
  • Use document.write to attach them
Both of these options will produce the same network traffic with the difference being in how the scripts are executed on the client. By loading the scripts in parallel we can cut down initial page load time drastically. The time for Script1.js to finish downloading will probably go up slightly over serial loading (since more of the connection is used) but the overall download time should decrease.
Network traffic for loading scripts in parallel 

Attaching scripts as elements dynamically


    <script type="text/javascript">

        function AttachScript(src)


            var script = document.createElement("SCRIPT");

            script.type = "text/javascript";


            script.src = src;





  <script type="text/javascript">





In this scenario IE and Firefox will download both scripts but Internet Explorer will also execute them in the order they finish downloading in whereas Firefox downloads them asynchronously but still executes them in the order they are attached in the DOM.

In Internet Explorer this means your scripts cannot have dependancies on one another as the execution order will vary depending on network traffic, caches, etc.

In Firefox you can mostly get away with keeping dependancies on Script1 from Script2 with the caveat that if the user presses stop after Script2 has finished downloading but Script1 has not, Firefox will execute Script2 and abort downloading Script1. Ensure that Script2 can properly handle Script1 failing to load.

Attaching scripts via document.write

<script type="text/javascript">

  document.writeln("<script type='text/javascript' src='Script1.js'><" + "/script>");

  document.writeln("<script type='text/javascript' src='Script2.js'><" + "/script>");


Using document.write produces the same effect in both Internet Explorer and Firefox: the scripts are downloaded in parallel but executed in the order they're written to the page. The same caveat above about the user stopping the page after Script2 has finished downloading before Script1 has applies.


This technique can be used to download more than two files at the same time as long as more than 1 hostname is used. IE will only open 2 HTTP connections to a named server in order to be a good client (Firefox will open 4) but you can easily work around this by creating additional domain names like, etc that point to the same host. At some point you will see diminishing returns and possibly even slowdowns from trying to download too many files at once so don't go too crazy with parallelizing.

Edit: Noted that Firefox will open 4 connections per named host by default.