Homam
Homam

Reputation: 23861

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

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>

Upvotes: 2

Views: 935

Answers (6)

neeebzz
neeebzz

Reputation: 11538

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 :)

Upvotes: 0

Genius
Genius

Reputation: 1782

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?

Upvotes: 1

AEMLoviji
AEMLoviji

Reputation: 3257

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

Upvotes: 0

x10
x10

Reputation: 3834

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.

Upvotes: 0

Kris van der Mast
Kris van der Mast

Reputation: 16613

  • 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.

Upvotes: 2

Van Coding
Van Coding

Reputation: 24554

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.

Upvotes: 0

Related Questions