Question: I have a script that knows to load dynamiclly scripts that contains javascript classes.
i'm loading the class script using the following code:

```
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "myscript.js";
head.appendChild(script);

```

i'm then trying to create the new class using eval:

```
var classObj = eval(" new MyClass()" );

```

the problem is that the code of the eval is being executed bofre the script has been loaded into memory and i get an error that the `MyClass is undefined.` 

Is there a way to synch these events? i need to make sure the script is fully being loaded into memory before i can start allocating classes from it.

Answer: 
Since you seem to be able to edit the external script (since you tested it with an alert), why not just put this code in that script?

If you can't do that (maybe the extra code is generated or the first file is shared perhaps), just add a function call at the end of the script you're loading like this:

```
load_complete();

```

and then put your extra code in that function:

```
function load_complete() {
    var classObj = eval(" new MyClass()" );
}

```

It's a lot simpler and foolproof than any kind of onload trigger. Also, if the js file is shared, then you can have different load\_complete functions on every page that uses it (just be sure to always define a load\_complete, even if it is empty).
Use the [jQuery](http://jquery.com/) (a JavaScript library) `[getScript](http://docs.jquery.com/Ajax/jQuery.getScript#urlcallback) function to load your script async. Use the callback function to create your objects. Example:`

```
$.getScript("script.js", function () {
  var classObj = new MyClass();
});

```