Javascript 如何动态加载带有属性的js文件并在加载时执行回调? 我试过的

Javascript 如何动态加载带有属性的js文件并在加载时执行回调? 我试过的,javascript,jquery,coffeescript,Javascript,Jquery,Coffeescript,jQuery的$.getScript做了其中两件事,但我不知道如何将属性添加到它添加到页面的标记中 我也试过这个(注意:在咖啡脚本中): 您可以通过AJAX将其作为文本获取,并将其输出到脚本标记的innerHTML属性中。然后在附加它之前(或之后),可以在该脚本元素上设置属性 var xhr = new XMLHttpRequest(); xhr.open("GET", "test.js", true); xhr.send(null); xhr.onreadystatechange = func

jQuery的
$.getScript
做了其中两件事,但我不知道如何将属性添加到它添加到页面的标记中

我也试过这个(注意:在咖啡脚本中):


您可以通过AJAX将其作为文本获取,并将其输出到脚本标记的
innerHTML
属性中。然后在附加它之前(或之后),可以在该脚本元素上设置属性

var xhr = new XMLHttpRequest();
xhr.open("GET", "test.js", true);
xhr.send(null);
xhr.onreadystatechange = function() {
    if (xhr.readyState != 4)
        return;

    var script = document.createElement("script");
    script.innerHTML = xhr.responseText;
    script.setAttribute(); //add attributes
    document.body.appendChild(script);

    console.log("callback!");
};
$ ->
  onAlohaLoad = ->
    console.log("aloha loaded")

  if localStorage["isAdmin"] == "true"
    alohaScriptUrl = "/plugins/alohaeditor-0.20.0-RC9/lib/aloha.js"
    alohaPlugins = "common/format"
    $("<script>", {src: alohaScriptUrl, "data-aloha-plugins": alohaPlugins})
      .bind("load ready", onAlohaLoad)
      .appendTo("head")
var xhr = new XMLHttpRequest();
xhr.open("GET", "test.js", true);
xhr.send(null);
xhr.onreadystatechange = function() {
    if (xhr.readyState != 4)
        return;

    var script = document.createElement("script");
    script.innerHTML = xhr.responseText;
    script.setAttribute(); //add attributes
    document.body.appendChild(script);

    console.log("callback!");
};