代码之家  ›  专栏  ›  技术社区  ›  Maxim Gershkovich

没有createElement的动态javascript注入

  •  0
  • Maxim Gershkovich  · 技术社区  · 16 年前

    因此createElement不能用于动态加载脚本。

    有没有人知道在这种情况下我应该如何“注入”代码?我能想到的唯一方法是使用一个webservice和一个JSON对象,然后我会对其进行评估,但这会产生一系列问题和复杂性,并在我的口中留下不好的味道。

    有什么想法吗?

    1 回复  |  直到 16 年前
        1
  •  1
  •   Sean Kinsey    16 年前

    无需使用webservice,只需使用纯XMLHttpRequest检索javascript文件,然后对其进行评估。

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