0

I fund this excelent post about including js file in each other How do I include a JavaScript file in another JavaScript file?

and would like to use the jq suggestion

client is using mootools and jquery libraries and currently has multiple JS files loading in head due to all plugins and libs (10 files) . I was able to compress all this in 2 files I have 1 file that is loading mootools , jquery, and Mootools plugins , another file that is running the mootools functions.

so the order is this

compressed.js
moofunctions.js

now dependiong on what page you are on , we are loading additional jq plugins so the order changes to

compressed.js
moofunctions.js
requested_jquery_plugin.js

so the questions I have are:

  1. do you see an implication with $.getScript() jq function ?

  2. since $.getScript() is actually loading additional script via ajax , is this script seen as http request or will my request come down to 1 js file? ( answered with FB, only 1 js request seen )

  3. the order in which the libs and plugins are compressed is in file 1 (compressed.js)

    mootools lib, jquery lib , mootools plugins,

at the end of this file I included

(function($){
        $.getScript("moofunctions.js", function(){


           console.log("Script loaded and executed.");

        });
})(jQuery);

do you see an implication with this?

any help is appreciated. Please don't advise/ask to use only 1 lib. Thnx!

Community
  • 1
  • 1
Benn
  • 4,840
  • 8
  • 65
  • 106

1 Answers1

1

How about turning this problem around and using a build script that concatenates the files in the correct order? I recommend using Grunt, Browserify or Clientside to rebuild the files using watch (rebuilds when files are saved) and using that built (and optionally minified) file in your <script src="..."> file.

We use this process at our company to compile and minify all our CoffeeScript code using CommonJS and so far it's worked a whole lot better than any client-side on-demand loading script.

EDIT: To answer your question of loading scripts dynamically, use RequireJS, it uses AMD modules to define dependencies and load them in the correct order.

Klemen Slavič
  • 19,661
  • 3
  • 34
  • 43
  • sounds great but il need big time reading to adjust to one of those :). not 100% in js , just a user I say. will take a look – Benn Aug 19 '12 at 20:54