How to use callback function in javascript?

I want to change this code to use a callback function. I would like the program to call the ExecuteScriptForEdit after calling DynamicLoadScriptForEdit(), but this code doesn't work.

window.onload = function() {
  DynamicLoadScriptForEdit('/temps/F.js', function(err) {
    ExecuteScriptForEdit();
  }); //네트워크가 좋으면 상관없지만 안좋으면 동기화가 실패, 콜백으로 가야함
}

function DynamicLoad(source) {
    var script = document.createElement('script');
    script.src = source;
    script.onload = function() {
        //do stuff with the script 
    };
    document.head.appendChild(script);
}

function DynamicLoadScriptForEdit(source, callback) {
    DynamicLoad(source);
    OnButton('ID_Button_Test');
}

Please help me. Thank you.


ANSWERS:


To achieve this you can pass the function you provide to DynamicLoadScriptForEdit() in the callback parameter to the lower level functions before calling it in the onload handler of the script. Try this:

window.onload = function() {
    DynamicLoadScriptForEdit('/temps/F.js', function(err) {
        ExecuteScriptForEdit();
    });
}

function DynamicLoadScriptForEdit(source, callback) {
    DynamicLoad(source, callback); // provide callback function
    OnButton('ID_Button_Test');
}

function DynamicLoad(source, callback) { // receive callback function here
    var script = document.createElement('script');
    script.src = source;
    script.onload = function() {
        // do stuff with the script 

        callback && callback(); // call here
    };
    document.head.appendChild(script);
}


 MORE:


 ? nodejs gridfs grid.put() with identical filename overwrites file
 ? process.env.NODE_ENV is undefined
 ? NodeJS + MongoDB + Redis: Register and login - sessions issue
 ? How does next() work in Node.js?
 ? Best way to crop and upload image with express?
 ? How to pass node.js objects to child process and then access those objects
 ? Setup a development server on linode
 ? How to output a twitter api respose using node js version 0.6.18
 ? Not able to install node module for SQLite 3 on Windows
 ? Bind event to all socket.io connections