2

Is it possible to include a folder of javascripts files instead of adding them one by one?

Something like this:

 <script language="javascript" type="text/javascript" 
     src='<%# Page.ResolveUrl("~/Scripts/?.js></script>

instead of:

<script language="javascript" type="text/javascript" 
 src='<%# Page.ResolveUrl("~/Scripts/jquery-1.5.1.min.js")  %>'></script>

<script language="javascript" type="text/javascript" 
 src='<%# Page.ResolveUrl("~/Scripts/jquery.localscroll-1.2.7-min.js")  %>'></s

<script language="javascript" type="text/javascript" 
 src='<%# Page.ResolveUrl("~/Scripts/jquery.scrollTo-1.4.2.js")  %>'></script>

<script language="javascript" type="text/javascript" 
 src='<%# Page.ResolveUrl("~/Scripts/jquery.simplemodal.js")  %>'></script>

<script language="javascript" type="text/javascript" 
 src='<%# Page.ResolveUrl("~/Scripts/jquery-ui-1.8.11.custom.min.js")  %>'></sc

<script language="javascript" type="text/javascript" 
 src='<%# Page.ResolveUrl("~/Scripts/jquery.noselect.min.js")  %>'></script>
1
  • Probably not inbuild, but you should be able to write a helper for this. Just iterate over all the nodes in the folder and inject them into the page. Commented Apr 21, 2011 at 11:22

6 Answers 6

2
  • For my former project we created a custom HttpHandler for this. Read out the content of the folder and injected that. Due to limitations however of having to make the scripts sorted for correct injection, we later improved it by creating a custom section in web.config. These held the correct order of how to inject the scripts.

  • Another solution's to minimize the scripts and to have them put into one file. Be sure to also read this article: Automatically minify and combine JavaScript in Visual Studio.

Sign up to request clarification or add additional context in comments.

1 Comment

I will recommend mix of both approaches i.e. use a http-handler that would serve the minified and combined java-scripts files.
1

Simple way:

<asp:Repeater ID="scriptsToInclude" runat="server"><ItemTemplate>
  <script language="javascript" type="text/javascript" src='<%# Page.ResolveUrl("~/Scripts/" + Container.DataItem + ".js") %>'></script>
</ItemTemplate></asp:Repeater>

And in codebehind:

protected void Page_Load(..) {
  scriptsToInclude.DataSource = new [] { "jquery-1.5.1.min", "jquery.localscroll-1.2.7-min", "jquery.simplemodal", ... };
  scriptsToInclude.DataBind();
}

Got an idea?

4 Comments

Yes, I got it. I think I'll user Directory.GetFiles insead for DataSource. Thanks.
@Jack Yes, Directory.GetFiles is a good approach, but be careful - there you can have problems with order of js files. If so - order them by some prefix, like 10_jquery.min.js, 20_jquery.localscroll.js, etc..
@Jack Also Directory.GetFiles returns an array with full pathed files. So don't forget to trim it by the website root path
Actually I haven't faced a problem in the order in js files before. But I'll consider it rightnow.
0

No because your browser doesn´t know the contents of the folder. If your browser would know the contents it would be very dangerous.

This is only possible with a helper program.

1 Comment

It's OK to be on the server side.
0

It is not possible, but it is very easy to compile a folder of .js files into one(possibly minified) file.

In fact, it's so easy, you can automate it and turn it into a part of the build process. Most people do.

One of the best tools for the job is the Google Closure compiler.

Comments

0

You can create one main file and include all your JavaScript files to this file. And then add this one file to page

Comments

0

You should try using Chirpy. It's a Visual Studio plug-in and great for combining all your Javascript files into one file and then sending it to the server. It'll not only save the hassle of adding each file but also would require only one HTTP request to get all the Javascript. Works for CSS too :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.