开发者

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="j开发者_如何学Goavascript" 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>


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


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?


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.


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜