0

The question initially arose from a idea to dynamically load a group of javascript files with $.getScript.

I am developing a js-only front-ends of a couple of projects, which share some libraries, and it would be nice (or at least it seems) to have a one js file which handles common libraries includes, and also the other one - which handles specific libraries.

Some of the front-ends I am talking about will actually work as widgets to the other websites, and until we setup a process of js file compression into one, that is also needed to place a widget as a single js include, and then include all the dependencies in a queue.

Something like: $.getScript .getScript .getScript

So the question has two parts: 1. Would it make sense? 2. If yes, how?

Thanks!

lyuba
  • 6,250
  • 7
  • 27
  • 37

1 Answers1

2

do you mean the following: you want to write something like

$.getScript("js/file1.js").getScript("js/file2.js").getScript("js/file3.js");

and as result load one file from server, which contains all specified (sort of file1.js + file2.js + file3.js), don't you? If I get it, then It seems to be impossible, at least that way. It really makes some sense, even good one, because browser will send one request instead of three (so performance will be better). To implement what you want, you need good support from server side. E.g. you could write

$.getScript("js/libraries?files=file1,file2,file3");

and server side should be able to parse this request, append all files mentioned in request, and send you. But as you see, something should exist on server side to assist you;

If you develop some framework or such, you can make a predefined build, e.g. your library will contain

js/
    file1.js
    file2.js
    file3.js
common.js

where common.js is just file1.js + file2.js + file3.js, and you can automate it, so you will change your code, and then run a special build which will create common.js. This way you can rely on

$.getScript("common.js");

and that's even better than requesting

$.getScript("js/libraries?files=file1,file2,file3");

because in case of last one some runtime job has to be performed (performance suffers a bit). when we develop our projects we use jawr on server side to make predefined bundles of javascript libraries/files

Maxym
  • 11,836
  • 3
  • 44
  • 48
  • Thank you for extensive answer. In our case back-end may be considered to be deployed separately from the front-end, and where the front-end is - there's basically only php. I could make a small php script for what you've proposed, but the js compression looks like a more interesting option. However, in this case I would expect the library work much like scss for css - do this in a runtime locally watching the changes in the js files. Would appreciate some tips on where to find it :) – lyuba Mar 03 '11 at 20:30
  • прошу ;) well, I don't know any library like jawr for php, so you really will need to write your own; JS compressors are discussed [here](http://stackoverflow.com/questions/28932/best-javascript-compressor), you can append script files, and then just use your favorite tool to compress (minify) them. Your server can be configured to zip responses (e.g. javascript), it also helps to minify traffic... – Maxym Mar 03 '11 at 20:39
  • after creating your bundle, you can save it on the file system; next time all you need is to check if your scripts where not changed after that (just check when files where [modified](http://php.net/manual/en/function.filemtime.php) and compare with bundle file creation time). Sort of... Otherwise which tips do you need? ;) – Maxym Mar 03 '11 at 20:42