One way to test this would be to do a console.log on the "onLoad" event and a console.log on your loadScript function. With your current code you'll see that onLoad is executing prior to loadScript.