开发者

Dynamic javascript injection without createElement

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 comp开发者_运维知识库lexities 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?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜