Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

DevWebProDK
FlashNewz
DevWebPro










Remove Generated JavaScript From Your Pages With ASP.NET

By Mads Kristensen
Expert Author
Article Date: 2007-10-22

One of the things that have always seemed a little weird to me is that ASP.NET auto-generates JavaScript and injects it in the rendered HTML.

The JavaScript is needed to handle validation, postbacks, callbacks etc. but why does it have to write the same static functions when it could just as well be placed in a referenced .js file?

If all the static functions where placed in an external .js file, it would be downloaded once instead of every time a page loads.

I thought I'd do something about it and wrote an HttpModule that removes and rewrites some of the auto-generated JavaScript.

Then I put the static functions into an external .js file and referenced that from the <head> section instead.

It also changes all document.getElementById(id) to $(id).

The result is a smaller and cleaner HTML output

I've implemented it on this website and if you take a peek at the HTML source you'll notice that you don't find functions such as __doPostBack and ValidatorOnSubmit along with some other JavaScript logic.

It has been moved to my global external .js file instead.

Implementation

Download the zip file below. It holds two files - an HttpModule and a JavaScript file.

You need to reference the .js file or copy the contents into your own external referenced .js file.

Then hook the HttpModule up in the web.config.

Comments

About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/



DevWebProDK is an iEntry, Inc. ® publication - All Rights Reserved Privacy Policy and Legal