I found another post from Nicholas Zakas interesting:

 

The best way to load external JavaScript

http://www.nczonline.net/blog/2009/07/28/the-best-way-to-load-external-javascript/

 

default.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Test page</title>
</head>
<body>



<script type="text/javascript" src="http://your.cdn.com/first.js"></script>
<script type="text/javascript">
    loadScript("http://your.cdn.com/second.js", function () {
        //initialization code
    });
</script>
</body>
</html>

first.js

function loadScript(url, callback) {

    var script = document.createElement("script")
    script.type = "text/javascript";

    if (script.readyState) {  //IE
        script.onreadystatechange = function () {
            if (script.readyState == "loaded" ||
                    script.readyState == "complete") {
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function () {
            callback();
        };
    }

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.