3

I have found several answers about this but in my undestanding they cover libraries located in same dir with calling file.

In my application, I currently have the following code inside index.html page head:

<script type="text/javascript"
          src="https://apis.google.com/js/api.js"></script>

What I want to do is load and use the same library inside a .js file. The application is client-side and is currently using the following frameworks: "serveup", "hem","es5-shimify", "json2ify", jqueryify".

I have tried using jquery 'require' but I got error message saying the module was not found, I am guessing it was looking for a local file.

Giannis
  • 5,286
  • 15
  • 58
  • 113
  • 1
    That Google api.js file adds the global `window.gapi` variable, so as long as the HTML file has all of its libraries, including that one, loaded before any of your JS files (and I'm understanding your issue correctly), you should be able to just use `gapi` in your own JavaScript and have all of its functionality, without having to directly include that JS file in your own file (since they're all loaded on the same HTML page). – ajp15243 Jun 26 '13 at 21:59
  • @ajp15243 it looks like you are right.. I am totally new to JS and have very short time to complete this project. Thanks for your comment. – Giannis Jun 26 '13 at 22:06

2 Answers2

2

Try this:

  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", "https://apis.google.com/js/api.js");
matt2000
  • 1,064
  • 11
  • 17
0

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.

PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56