1

We're looking for a way to manage ever-increasing javascript code for our asp controls (used with RegisterClientScript family). The classic way was to have javascript encoded into string constants inside c# code but now it's getting bigger and you don't want to develop inside c# string. Having code in external file and convert/copy it into a string form before compilation is also somewhat ridiculous. Plus in these scenarios almost same code is sent several times with each instance of the control.

Other important criteria is to have the scripts self-sufficient, with only external dependency on jquery&co, so having control-specific javascript files somewhere else is not desired. All the relevant parts have to be in one place.

My personal best option so far is to have Javascript split into two parts:
- "code behind"-like js file compiled as embedded resource into assembly. This is about 3 times slower than string concatenation but. Plus it brings options for caching and direct-from-file without recompiling for debugging. This part is delivered inline once per page containing the control.
- tiny initialising proxy function calling the main script with control-instance specific parameters. These are delivered also inline but once per control instance.

I'm sure we're not the only ones scratching our heads about this.
So how do you do that?

Update. Getting rid of .net specifics, situation is following. We have some 100+ reusable server-side components each possibly containing some javascript client-side functionality. These controls are of course not used in every page so packing the javascript into one common lib is not desired. On the other hand, having 20+ references to external javascript files each being 1-2Kb in size is also both headache for maintenance and traffic overkill. That is why we want to have the component know and care about its own javascript inclusion.

On the other hand, if you have a project where these components are spread into several libraries, you don't really want for them to depend on some particular filesystem layout which may be of course standardised but it's too much of responsibility for a vehicle tire to enforce your cola choice in a diner, imo.

1 Answer 1

2

If you have JavaScript code that is re-used by several controls - why don't you want this in an external *.js file? This will allow you to maintain formatting, and yet still serve up a minimized, gzipped and client side cachable file.

3
  • I second this.. external JS files are the way to go in my opinion, and if you need any client side data coming from the server code behind, add it as global variables using "RegisterClientScript family" as you call it. Nov 1, 2010 at 10:25
  • my current best option is exactly to have it in external file in the project while editing, but as embedded resource in assembly during runtime(with an option to serve it from filesystem when debugging) so I'm completely agree with you about external files. We're just not very happy about deploying dll file and then something else (and eventually forgetting to or making mistake).
    – durilka
    Nov 1, 2010 at 10:44
  • I'm not overly familiar with .net development but in order to gain the all the benefits of compact cachable JavaScript you need to put it into external *.js files. I would recommend that they begin and remain as external files, and where you include them reference them with a version number so that they break the cache when necs. otherwise they cache for a very long time. Deployment should include all your static content js/css/images every time (optimized to add/update as needed).
    – scunliffe
    Nov 1, 2010 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.