vote up 5 vote down star
5

I'm jealous of the rails guys. They can do this:

<%= javascript_include_tag "all_min" %>

... and I'm stuck doing this:

<script src="/public/javascript/jquery/jquery.js" type="text/javascript"></script>
<script src="/public/javascript/jquery/jquery.tablesorter.js" type="text/javascript"></script>
<script src="/public/javascript/jquery/jquery.tablehover.pack.js" type="text/javascript"></script>
<script src="/public/javascript/jquery/jquery.validate.js" type="text/javascript"></script>
<script src="/public/javascript/jquery/jquery.form.js" type="text/javascript"></script>
<script src="/public/javascript/jquery/application.js" type="text/javascript"></script>

Are there any libraries to compress, gzip and combine multiple js files? How about CSS files?

flag

5 Answers

vote up 5 vote down check

Also have a look at this article on codeproject:
http://www.codeproject.com/KB/aspnet/HttpCombine.aspx

link|flag
vote up 7 vote down

You can use a ScriptManager/ScriptManagerProxy control and define the scripts in the CompositeScript section/property. See MSDN reference.

<asp:ScriptManager runat="server">
    <CompositeScript>
        <Scripts>
            <asp:ScriptReference Path="~/public/javascript/jquery/jquery.js" />
            <asp:ScriptReference Path="~/public/javascript/jquery/jquery.tablesorter.js" />
            <asp:ScriptReference Path="~/public/javascript/jquery/jquery.tablehover.pack.js" />
            <asp:ScriptReference Path="~/public/javascript/jquery/jquery.validate.js" />
            <asp:ScriptReference Path="~/public/javascript/jquery/jquery.form.js" />
            <asp:ScriptReference Path="~/public/javascript/jquery/application.js" />
        </Scripts>
    </CompositeScript>
</asp:ScriptManager>

It doesn't necessarily clean up the markup any, but it does zip them together.

link|flag
vote up 2 vote down
<%= javascript_include_tag "all_min" %>

That really has all the semantics of a classic asp function call, even if it's really ruby. In fact, not knowing any ruby I can still be pretty confident with the guess that this is just a function and "all_min" refers to a folder name that's being passed in as an argument.

Since the <%= %> bee-stings are just a short-cut for Response.Write in classic ASP, we can conclude that you ought to be able to build your own function that does essentially the same thing and returns a string with the relevant includes.

link|flag
You are correct, in Ruby you don't need parens to pass a value to a function. – Adam Lassek Nov 19 '08 at 15:05
You must to be sure scripts are correctly ordered no ? – labilbe Nov 21 '08 at 16:22
@labilbe: I assume in that case you've followed a naming convention to make sure they're picked up in the right order. I doubt the ruby function is smart enough to check dependencies either. – Joel Coehoorn Nov 21 '08 at 21:52
vote up 1 vote down

You can do this using an HTTP handler. Check out this blog post from Mads Kristensen:

Combine multiple stylesheets at runtime

link|flag
Seems like overkill when the whole point it to simplify things. On the other hand, I suppose he only has to write the handler once and he can port it around from site to site. – Joel Coehoorn Nov 18 '08 at 22:21
That's completely different than the OP's question. The article talks about combining multiple requests into a single one a runtime, but you still must list them each separately. – Adam Lassek Nov 18 '08 at 22:22
vote up 1 vote down

ScriptManager is under BSD licenece and this I dislike :(. You may see a very good alternative how this is implemented in KiGG's approach:KiGG

The idea behind is that the control allows you to join the files js from web config by separating them into categories(you enlist their names ) pretty simple yaeh. good luck.

link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.