CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Karl Seguin

developer @ Fuel Industries ottawa, ontario

Javascript Management with ASP.NET

Over the weekend, I dug up an ASP.NET article I wrote a while ago and never put up anywhere. It was actually meant as a practical example of using HttpHandlers - which I've talked about before.

Basically, the idea is to store of JavaScript into a store (I use XML). Have the JavaScript grouped up into like functionality, such as "Tabbing", "BrowserDetection", "MouseOver", etc. The a page developer simply says, "I need Tabbing on this page", ala JavaScript.EmitJavascript(JavascriptType.Tabbing, JavascriptType.BrowserDetection);

What's really cool about the article is that is uses Dean Edward's Javascript packer. So blocks of JavaScript are obfuscated on the fly (and cached). That means that in development you're dealing with easy-to-read JavaScript, but in production it's compact code.

For example, if you put the HUGE atlas.js (239Kb) file through it, it'll come out at just about 1/2 the size (roughly 123kb). That's insane!

You can read the article here.

Technorati Tags: ,






Comments

Greg said:

One really useful bit of functionality that I didn't see is the ability to embed that XML file as a resource. In other words we can ship all of our javascript as a single .dll file  (no supporting files such as .xml configurations, when dealing with signed dlls we can also ensure that people are not changing this information..).

Many systems use this setup when dealing with httphandlers that handle images as opposed to distributing an images folder with the application.
# July 2, 2006 5:26 PM

Atul said:

Karl,
    This is a neat use but with this approach the Browser file caching feature will not be used. Correct me If I am wrong.
# July 3, 2006 5:48 AM

karl said:

Atul:
That's right. It's still possible to set the Expiry headers via the response in the httphandler. Of course, if you output blah.ashx?id=3&id=4  and then just blah.ashx?id=3 it won't know that the code block in id=3 was already cached...definetly a disadvantage of this approach.
# July 3, 2006 11:36 AM

Atul said:

Karl,
     We have a HUGE javascript file (bigger then atlas). We were using JsMin earlier which was compressing it by 30%. I am using the Javascript packer for the last two days. Thanks for the pointer.
# July 4, 2006 3:10 AM

Dave said:

I can tell you what approach you shouldn't use. - Multiple JS Files.

I had a jsfile for my calendar control, a jsfile for my dynamic grid, a jsfile for my dhtml buttons, a jsfile for my menu etc.... So depending on what page a user hit it may link from anywhere from 1 - 10 js files. I thought I was actually helping out people with slow browsers. - They only get what they need.

Here's what actually happens. Sometimes IE will load 7 of 8 files and for some reason bombs on one of the others. - just one load. Sure this may only happen 1 out of 1000 requests but it does happen and its very annoying.

We've all seen this on things like broken images. Where all we do is refresh the page and viola! its there.

I'm a believer in the one big fat js file. They get it all once and the browser can cache it.
# July 5, 2006 5:08 PM

scottgu said:

Atlas includes a built-in HTTP compression module for resources served up via ASP.NET's webresources.axd handler.

If you run in release mode (not debug), it will compress your .js files for you.  The full Atlas .js framework currently compresses down to 46k (about 1/4th the size) with it enabled.

Hope this helps,

Scott
# July 5, 2006 8:16 PM

ATLAS Forum Posts said:

Hi folks, I saw an article states that Atlas includes a built-in HTTP compression module, which able
# August 14, 2006 6:54 AM

Pablo said:

Hi, I have a question. This code is usefool or not with atlas?

Regards

# December 20, 2006 1:49 PM

karl said:

Probably not too much, as Scott mentioned, Atlas has built-in compression and already supports fairly clean management functionalities.

# December 20, 2006 6:09 PM

jWj2k said:

The "Javascript Management with ASP.NET"  article was a huge help for me as I make the transition to Classic ASP to ASP.NET. The sample that was presented in the article R-U-L-E-S and I learned quite a bit from it. I offer a heartfelt thanks for being generous enough to share your knowledge. You really helped me out with this sample.

# November 15, 2007 10:10 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!