0

Is there a way to link multiple javascript files without making them one file? What I would like is to have one file (javascript or otherwise) which houses links to my other javascript files.

For example, the webpage has one file called allmyscirpts.js, and inside this file is a list of links to my actual individual, separataed javascript files.

Is this possible?

Tod

tod
  • 1
  • 1
  • http://stackoverflow.com/questions/21294/how-do-you-dynamically-load-a-javascript-file-think-cs-include **OR** http://stackoverflow.com/questions/950087/include-javascript-file-inside-javascript-file – Ivan Jun 02 '11 at 22:14
  • why would you want a link to your other files? why wouldn't you just load them all? javascript doesn't care which file it's in. – Jason Jun 02 '11 at 22:14
  • I have a client who keeps screaming out "too many javascript files are bad", and every time I try to explain that it isn't that big a deal, he just repeats stuff he's read on the www. So I thought if there was a way to indulge his request, I might try it. Will look at all the great suggestions provided here :) – tod Jun 02 '11 at 22:36

2 Answers2

0

JS can't simply import more JS, but you could easily write a simple server-side script that concatenates your files together. If you can't/won't work on the server, scriptloader libraries are very plentiful out there these days. Check out require.js, lab.js, yepnope.js, etc. and see if one of them suits you well.

RwwL
  • 3,298
  • 1
  • 23
  • 24
0

The only way I can think of is to load Javascript files through ajax. The YUI Loader you to not only load all your js files (and those from YUI) within javascript, but it also allows you to configure dependencies between your js files. So For instance, if widget1.js requires global.js, you can configure that dependency, then you can tell the loader to load "widget1" and the loader will also load global.js when it loads widget.js.

Unlike css, I do not believe there is built in syntax in javascript that automatically includes another javascript file. But there are javascript utilities out there that allow this.

For a simpler solution than the YUI Loader, check out the YUI get utility. For my projects I have setup the YUI loader, and as a result my HTML pages only have about 2 or 3 javascript files included, and the rest of what I need is loaded on demand by the Javacript controller for that page.

Zoidberg
  • 10,137
  • 2
  • 31
  • 53