Dynamically load a JavaScript file

后端 未结 28 3360
说谎
说谎 2020-11-22 06:56

How can you reliably and dynamically load a JavaScript file? This will can be used to implement a module or component that when \'initialized\' the component will dynamical

28条回答
  •  执念已碎
    2020-11-22 07:36

    Here is some example code I've found... does anyone have a better way?

      function include(url)
      {
        var s = document.createElement("script");
        s.setAttribute("type", "text/javascript");
        s.setAttribute("src", url);
        var nodes = document.getElementsByTagName("*");
        var node = nodes[nodes.length -1].parentNode;
        node.appendChild(s);
      }
    

提交回复
热议问题