Ads Inside Post

Search in This Blog

load javascript with callback script

How to load JavaScript with callback script

Sometime you needed to write a javascript that load and after load immediately call other it's related javascript code. It is called load javascript with callback script.

How to achieve it?
It is simple. below is the few lines of code.

<script type="text/javascript">
function loadmyScript(url, callback){
    var sc = document.createElement("script")
    sc.type = "text/javascript";

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

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

function aa(){
loadmyScript("//www.com/javascript.js", function(){
// add here callback javascript code


});
}
aa();
</script>



below is one line of JavaScript code that check your system internet connect On of OFF.