views:

32

answers:

1

When running code in the context of a "thread" in the Google Gears API you do not have access to the "document" object and therefore createElement cannot be used to dynamically load a script.

Does anyone have any ideas about how I might go about "injecting" code in such a scenario? The only method I can think of is using a webservice and a JSON object which I would then eval but this creates its own series of problems and complexities and leaves a bad taste in my mouth.

The problem is I am ending up with large monolithic bits of code and various duplication of code due to this limitation and there is nothing that I hate more.

Any ideas?

+1  A: 

No need to use a webservice, just use plain XMLHttpRequest to retrieve the javascript file, and then eval it.

var xhr = new XMLHttpRequest();
xhr.open("foo.js", null, false); // since this is in a thread you can use the synchronous approach
xhr.send("");
eval(xhr.responseText);
Sean Kinsey
By the way, if you want to have the code eval'ed in the global scope do this `var fn = new Function(xhr.responseText);fn();`
Sean Kinsey