Can .js file "include" another .js file

This question already has an answer here:

  • How do I include a JavaScript file in another JavaScript file? 50 answers
  • Including a .js file within a .js file [duplicate] 5 answers

  • If you mean in a browser context, not directly, but you can use a loader like at RequireJS (there are several).

    Or to do it manually:

    var script = document.createElement('script');
    script.src = "/path/to/the/other/file.js";
    document.getElementsByTagName('script')[0].parentNode.appendChild(script);
    

    Note, though,that with the above the functions and such in the other file won't be available for use right away. The code above starts the process of loading the file, but it continues asynchronously. You can use events to know that the load is complete (primarily the load event, but on older versions of IE you have to use onreadystatechange -- this is one reason people use loaders!).

    链接地址: http://www.djcxy.com/p/7336.html

    上一篇: 在JavaScript文件中包含JavaScript文件

    下一篇: 可以.js文件“包含”另一个.js文件