Combine multiple script files into one request with ScriptManager in ASP.NET

No.of Views2103
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  30 Dec 2010 10:12:47
Tag : ASP.NET , Application Performance Optimization
We all need java script for our web application and we storing our JavaScript code in .js files. Now If we have more then .js file then our browser will create a new request for each .js file. Which is a little overhead in terms of performance
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

We all need java script for our web application and we storing our JavaScript code in .js files. Now If we have more then .js file then our browser will create a new request for each .js file. Which is a little overhead in terms of performance. If you have very big enterprise application you will have so much over head for this. Asp.net Script Manager provides a feature to combine multiple JavaScript into one request but you must remember that this feature will be available only with .NET Framework 3.5 sp1 or higher versions.

Implementation

Let’s take a simple example. I am having two javascript files Jscrip1.js and Jscript2.js both are having separate functions.

Javascript

//Jscript1.js
function Task1() {
   alert('task1');
   }

Here is another one for another file.

Javascript

////Jscript1.js
function Task2() {
   alert('task2');
}

 Now I am adding script reference with script manager and using this function in my code like this.

Html Code

<form id="form1" runat="server">
	<asp:ScriptManager ID="myScriptManager" runat="server" >
          <Scripts>
              <asp:ScriptReference Path="~/JScript1.js" />
              <asp:ScriptReference Path="~/JScript2.js" />
          </Scripts>
      </asp:ScriptManager>
      <script language="javascript" type="text/javascript">
          Task1();
          Task2();          
      </script>
  </form>

Now Let’s test in Firefox with Lori plug-in which will show you how many request are made for this. Here is output of that. You can see 5 Requests are there. 

Image Loading

Now let’s do same thing in with ASP.NET Script Manager combined script feature. Like following,

Html Code

<form id="form1" runat="server">
       <asp:ScriptManager ID="myScriptManager" runat="server" >
           <CompositeScript>
               <Scripts>
                   <asp:ScriptReference Path="~/JScript1.js" />
                   <asp:ScriptReference Path="~/JScript2.js" />
               </Scripts>
               
           </CompositeScript>
       </asp:ScriptManager>
       <script language="javascript" type="text/javascript">
           Task1();
           Task2();          
       </script>
   </form>

Now let’s run it and let’s see how many request are there like following.

Image Loading

As you can see now we have only 4 request compare to 5 request earlier. So script manager combined multiple script into one request. So if you have lots of javascript files you can save your loading time with this with combining multiple script files into one request.

Conclusion

In this article, you have learned reduce no of request with combine multiple JavaScript files into one request using ScriptManager. Hope you liked it. Please post your comments.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Gold
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top