Articles from October 2006

Managing JavaScript

By Michael Flanakin @ 9:10 PM :: 476 Views :: Patterns & Practices :: Digg it!

Question

I was wondering if when developing custom controls in asp.net that need client script if it is better to emit the script inline with the code or to use a script file that the page can refer to. Any ideas would be much appreciated.

Answer

Personally, I see three options. In preferencial order, they are (a) a separate JS file; (b) a cache-enabled ASPX file; and, (c) inline scripting. Use a JS file in any case where your script is static. The reason behind this is that modern web browsers will cache these files and ensure your app performs as best as possible -- remember that JavaScript is parsed when executed, so every little bit helps. If you require dynamic content, try putting the script in a separate ASPX page that utilizes cache headers to maximize the cacheability. In the event that you have code that is dependant upon server controls, use inline scripting. Even if you do go this route, tho, I'd suggest calling functions that are specified in cache-controlled locations to minimize page size and maximize performance.

On a side note, if you have no more than 5-10 lines of code, I wouldn't worry about it. Just stick it on the page since that's the simplest method. The real reasons to separate script into its own file is for performance and/or maintainability... well, and all the other *ilities, as well :-)





 Sightings

ASP.net

CodeProject Gold Level Member