Luke Smith Write today, wrong tomorrow

I want javascript code behind

ASP.NET provides a great feature, code behind files, where the high level language
(C#/VB.NET) can sit in a separate file to the UI markup of the page.

But what about Javascript? Well you create a javascript file and shove it in a folder,
I tend to use /scripts, and then add a script include reference to it, or put the
javascript within the HTML page. The problem with putting it in an external file
is you can't use <%= control.UniqueID %> to get the ID of a control
on that page after its rendered (you would have to 'hardcode' that id). If the javascript
is with the HTML you can use <% %> as ASP.NET parses it and puts the proper
ID in place.

Solution? Well what I'd like to see is a javascript code behind, mypage.aspx.js just
as with a cs code behind. The js file gets automagically included when the page is
requested and the javascript file is parsed for <% %> and voila. You get the
caching benefits and maintable code that having an external javascript file gives.

UserControls could use the same approach and add the javascript include to the page.

In debug="false" mode ASP.NET could optimize the javascript too to remove comments
and un needed formatting reducing the file size.

Any thoughts?

comments powered by Disqus