When the defer attribute is present, the script(s) will not execute until after the entire page’s code has been loaded.
Deferred scripts execute before the DOMContentLoaded event and should execute in the order in which they appear in the document (this isn’t necessarily the case in Internet Explorer). The defer attribute is supported in Internet Explorer 4, Firefox 3.5, Safari 5, and Chrome 7.
The second is to use the HTML5 async attribute on <script> tags. Async scripts also begin to download immediately and in a non-blocking manner:
The difference between async and defer is that async scripts execute as soon as the script is downloaded, so the page may still be loading when the script actually executes. Another difference is that the order of execution for async scripts is explicitly not guaranteed, so an async script that appears later in the document might actually execute before one that appeared earlier in the page. The only guarantee is that async scripts will execute before the load event.
When inserting a script dynamically, the non-blocking download begins immediately. The script executes as soon as it is downloaded completely. In most browsers, the order of execution is not guaranteed, though Firefox < 4 and Opera will execute the scripts in the order in which they were inserted. This general approach is supported in all major browsers.
window.onload event handler until all scripts have executed. All browsers obey this behavior except for Internet Explorer (even through version 9).