Click here to Skip to main content
Licence CPOL
First Posted 5 Nov 2007
Views 34,602
Downloads 112
Bookmarked 14 times

JavaScript File Compressor for ASP.NET Applications

By | 5 Nov 2007 | Article
Compress JavaScript files using this HTTP Handler.

Introduction

This article explains how to compress a js file while rendering by the browser.

Using the code

Download the code and build the DLL. Create a Website, and add a reference of the DLL to the Website. In the Web.Config file, add a new HTTP Handler in the HttpHandlers section:

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

That's all. The rest of the work will be done by our handler. Now, let us see how it works.

Basically, the HttpHandlers are used to handle requests of a Web Application. This handler is used to handle the JavaScript file. It removes white spaces, new line characters, inline comments, and multiline comments. The Handler class is inherited from the IHttpHandler Interface, and it implements the 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 he can still do it if he has patience:).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

asithangae

Web Developer

India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNice idea PinmemberNuri Kevenoglu13:31 13 Nov '07  
I have three ideas/suggestions:
 
1) Have you thought about using regular expressions?
 
2) Since the JS file is being processed, you could add your own logic inside the JS file which can be parsed in your server code. For example, you could render JavaScript for different DOMs using conditions like this:
 
var http;
<! If IE !>
http = new ActiveXObject("Microsoft.XMLHTTP");
<! Else !>
http = new XMLHttpRequest();
<! End If !>
 
etc..
 
I know you can do this purely in Javascript but doing it like above will add extra secuity to your Javascript because the javascript downloaded from one browser will not work in another.
 
3. Now what would be even more interesting is a server-side parser for CSS files...

 
Cheers,
Nuri
Generalbroken: var myvar -> varmyvar Pinmemberrobrich17:44 11 Nov '07  
GeneralClosing resources PinmemberDewey10:14 8 Nov '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 6 Nov 2007
Article Copyright 2007 by asithangae
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid