View Full Version : [Solved] JavaScript generation script

May 7th, 2008, 03:13 PM
Hello everyone.

I've been looking for a good JavaScript discussion forum for a while. I only just found this one - looks pretty good :).

Here's what I want to discuss:

It makes symantic sense to put each separate object in it's own JavaScript file. An example of this is the rather over-namespaced YUI JavaScript library (http://developer.yahoo.com/yui/).

However when you get into creating large JavaScript applications you can be dealing with many different objects, meaning that you could be making 15 or 20 links to external JavaScript file. Now since each new connection takes extra time this can slow down the loading of a web page quite significantly, and if these files are on every page of a website this could mean the whole site running considerably slower.

<title>My page</title>
<script type="text/javascript" src="/js/ParentObject.js"></script>
<script type="text/javascript" src="/js/ChildObject1.js"></script>
<script type="text/javascript" src="/js/ChildObject2.js"></script>
<script type="text/javascript" src="/js/AnotherObject.js"></script>
<script type="text/javascript" src="/js/SomethingElse.js"></script>

It seems to me that the best solution to this is to write a CGI script that can pull the different JavaScript files together and serve them as one file, thus only one connection has to be made to get all the JavaScript, but the JavaScript can still be separated into logical files:

<title>My page</title>
<script type="text/javascript" src="/scripts/loadJavaScript.cgi?ParentObject.js&ChildObject1.js&ChildObject2.js&AnotherObject.js&SomethingElse.js" />

Of course the URL could get rather long. So maybe you could include configurations, and tell it to load "configuration1" of the JavaScript files or something (e.g. /scripts/loadJavaScript.cgi?config=1) and then this would allow for the output of a configuration to be cached, cutting down on server-side processing time.

Anyway, I cannot be the first person to think of this. It's a well known problem so there must be a common solution. So my question is, how do people normally solve this problem? And is there a standard script similar to the one disctribed above that I can use? Or is there a better solution?


May 7th, 2008, 04:26 PM
Read http://www.sitepoint.com/blogs/2007/04/10/faster-page-loads-bundle-your-css-and-javascript/

May 7th, 2008, 04:36 PM
Cool thanks that is exactly what I was talking about. And it looks like they've done it in almost exactly the way I was suggesting. :)

Out of interest, do you use this sort of technique?

May 7th, 2008, 04:51 PM
I had tried it, and got it worked. But then after I removed, since I had some css& javascript files in some other directories other than /css & /javascripts , so I felt it hard to control. But I'm confident :)