|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
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
IntroductionCompress js file while rendering by browser Using the codeDownload 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.
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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||