5,427,303 members and growing! (17,648 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Advanced

js file Compressor for ASP.Net applications

By asithangae

Compress Javascript file,
Javascript, XMLWindows, .NET, .NET 2.0, WinXP, Win2003, .NET 3.0, .NET 3.5, ASP.NET, Ajax, VS2008, VS2005, Visual Studio, Dev

Posted: 6 Nov 2007
Updated: 6 Nov 2007
Views: 14,256
Bookmarked: 7 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 0.64 Rating: 1.33 out of 5
2 votes, 66.7%
1
1 vote, 33.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Compress js file while rendering by browser

Using the code

Download the code, and build the dll.

Create a WebSite, or Add a reference of the dll to the WebSite.

In Web.Config file a new httpHandler in HttpHandlers Section.

<add verb="*" path="*.js" validate="false" type="ClassLibrary1.Handler, ClassLibrary1, Version=1.0.0.0, Culture=neutral" />

That's all, rest of the work will be done by our Handler.

Let us see how it works,

Basically the HttpHandlers are used to handle the request of the WebApplication.

This Handler is to handle the javascript file.

it removes the white spaces, new line characters, inline comments and multiline comments.

the Handlers class is inherited from the IHttpHandler Interface, it implements ProcessRequest method.

//Getting the script file name from request 

Uri url = context.Request.Url; 
string filename = url.Segments[url.Segments.Length-1]; 

//Creating a file stream to read the script file. 

FileStream fs = new FileStream(context.Server.MapPath(filename), FileMode.Open); 
StreamReader sr = new StreamReader(fs); 
string js = sr.ReadToEnd(); 
string a = string.Empty , b = string.Empty; 
//Removing the single line comments 

while (js.IndexOf("//")!=-1) 
{
 a = js.Substring(0, js.IndexOf("//"));
 b = js.Substring(js.IndexOf("\r\n", js.IndexOf("//")));
 js = a+b; 
} 
//Removing multiline comments 

while (js.IndexOf("/*") != -1) 
{
 a = js.Substring(0, js.IndexOf("/*"));
 b = js.Substring(js.IndexOf("*/", js.IndexOf("/*"))+2);
 js = a + b; 
} 
//To Remove Blank spaces 

js = js.Replace(" ", string.Empty); 
//To remove Carrige retun and new line character 

js = js.Replace("\r", string.Empty); 
js = js.Replace("\n", string.Empty); 
//Flushing it in response 

context.Response.Write(js); 
//Closing the resourses used 

sr.Close(); 
fs.Close(); 
sr.Dispose(); 
fs.Dispose(); 

This makes the file compressed, and if any intruder tries to access your code, he will feel difficult to trace back the functionalities since the indentation and comments are missing. But still he can do it if he have patience:).

History

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

asithangae


Hai, I am a .Net professional just started my career in this field. I like to code more on asp.net. In my spare time, l like to write article and write blogs. i love music and intrested in watching movies.

you can view my blog here.
Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralNice ideamemberNuri Kevenoglu14:31 13 Nov '07  
Generalbroken: var myvar -> varmyvarmemberrobrich18:44 11 Nov '07  
GeneralClosing resourcesmemberDewey11:14 8 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Nov 2007
Editor:
Copyright 2007 by asithangae
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project