JavaScript JavaScript “include” function? javascript include function,javascript include function from another file,javascript

I was wondering if there is a javascript "include" function (similar to the one in python), and I was looking for something but couldn't find anything except $.load() and google.load().

So, I ventured out to create a script which could do that just for fun, so:

var include = function( lib ) {
// start with jQuery
    if(lib == "jquery") {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://code.jquery.com/jquery.min.js";
        script.defer = "defer"; // should I use script.setAttribute('defer', 'defer')?
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(script, s);
    }
}

And then, in a separate script:

include("jquery");
$("p").innerHTML = "Worked!";

But I got an error $ is not defined

Which makes sense, because the script is running before jQuery is loaded. So my question is, is there a way to make sure the include script runs before anything else ahead of it? I've seen callback solutions that look something like this:

include("http://code.jquery.com/jquery.min.js", function() {
    $("p").innerHTML = "Worked!";
});

But I do wonder if there is anything (like I proposed above) that's a little more neat.

Any help would be much appreciated!

Answer:1

Because of how JavaScript runs there is no way to do it much cleaner then that. You could have some sort of interval to load all files you need, so like this.

window.includeFiles = [];
function include(file){
   window.includeFiles.push(file);
}
function initialize(callback){
   for(var i = 0; i < window.includeFiles.length; i++){
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.src = window.includeFiles[i];
      script.defer = "defer"; // should I use script.setAttribute('defer', 'defer')?
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(script, s);
      if((i+1)==window.includeFiles.length){
         callback();
      }
   }
}

Basically this will let you co like so:

include("http://code.jquery.com/jquery.min.js");
include("http://code.jquery.com/jquery.min.js");
include("http://code.jquery.com/jquery.min.js");
include("http://code.jquery.com/jquery.min.js");
include("http://code.jquery.com/jquery.min.js");
initialize(function(){
  //code ready
});

Only problem is it does not test network, witch can only really be done by checking if a function only available in the included library is available. Witch makes raina77ow's answer to use RequireJS a better solution.

Answer:2

I have a web page in http://enlaces.webcindario.com/paraprobar02.htm that everything works fine. But when I press the CHECK ANSWER button shows the message at the top of the page. Also when you ...

I have a web page in http://enlaces.webcindario.com/paraprobar02.htm that everything works fine. But when I press the CHECK ANSWER button shows the message at the top of the page. Also when you ...

  1. button onclick shows border

So I have certain profile pages that needs to have page views. I stored the view data in mysql and php (actually laravel). Basic idea is to add view by 1 every time the profile is visited. Basic idea ...

So I have certain profile pages that needs to have page views. I stored the view data in mysql and php (actually laravel). Basic idea is to add view by 1 every time the profile is visited. Basic idea ...

I'm trying to inject some scripts into a page that I'm loading into PhantomJs using the phantomjs-node bridge. When I inject MochaJs into the page, I can evaluate and the window.mocha object is ...

I'm trying to inject some scripts into a page that I'm loading into PhantomJs using the phantomjs-node bridge. When I inject MochaJs into the page, I can evaluate and the window.mocha object is ...

I am facing difficulty to bind jstree with an on-expand/on-collapse event. I am currently binding the jstree with selected_node event like this : $(function () { $("#tree").jstree({ "json_data" : { ...

I am facing difficulty to bind jstree with an on-expand/on-collapse event. I am currently binding the jstree with selected_node event like this : $(function () { $("#tree").jstree({ "json_data" : { ...

  1. bind jstree
  2. binding jstree with database in asp.net mvc