How to use external(http) library in a JS file

This question already has an answer here:

  • How do I include a JavaScript file in another JavaScript file? 51 answers
  • How to include js file in another js file? [duplicate] 4 answers

  • 尝试这个:

      var fileref=document.createElement('script');
      fileref.setAttribute("type","text/javascript");
      fileref.setAttribute("src", "https://apis.google.com/js/api.js");
    

    I'm a little fuzzy on this myself, but I seem to recall that scripts can't easily call scripts from other domains, in order to avoid violating the "single source" rule.

    The typical techniques are to either do a ajax request, followed by an eval, or inject a <script> tag into the header, which causes the browser's interpreter to fetch the resource, but might be a little late in the execution process depending on what you are hoping to accomplish.

    What I've done for this is to use the Require.js framework, which handles all the various oddities of the various sandboxes. It's a little tough to get your head around, but maybe worth it.

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

    上一篇: 如何在自己的脚本中包含JavaScript库

    下一篇: 如何在JS文件中使用外部(http)库