Keep your javascript away from my markup
Today, many web applications use alot of javascript to create a nice experiance for the user. With libraries like jQuery it is very easy to create that little extra that gives you this "woow" feeling. I use alot of javascript as well and I think it´s pretty fun writing when using a library like jQuery. One problem, however, that I often see in applications is that the html markup is depenting on the javascript. If the javascript doesn´t work correctly, then the markup is screwed up.
In my opinion javascript should be there to extend existing markup, not creating it. I tend to use a ".js file per view" approach while developing. Where I first create my view and make sure it work the way it´s supposed to without javascript. I then add a js file for that view and change the behaviour of some functionality. For instance I could do a asyncronas post instead of a normal form post or I can display some data in a popup instead of in a new page. This way you make sure you have unobtrusive javascript code and the javascript code is seperated from your html. I tend not to declare my events (like OnClick) directly in the markup, I´d much rather declare it seperatly. You can do this very easily with jQuery, like this:
.......
<input type="text" id="TextBox" />
.......
<script type="text/javascript">
$("#TextBox").click(function() {
//Do something
});
</script>
To me that looks alot better then this:
.......
<input type="text" id="TextBox" OnClick="CallFunction();" />
.......
<script type="text/javascript">
function CallFunction() {
//Do something
}
</script>
This way you can read your .js file and understand most of what it does without looking at the markup and the view will be a bit cleaner and contain only html markup as it should, in my opinion. I see javascript as a layer above the html that you use to tweek the markup in a desirable way. I like to keep javascript in a file of it´s own, but that is just a personal preferance and there is nothing wrong with puting it in the view, as long as it is in the bottom where it belongs.
Don't agree with you there, I think it's okay to have a javascript block in your view as long as it separate from the html code as you said and it's at the end of the page. Why then??? well if you want to do nice stuff like this guy link. Here he creates his own htmlhelpers to generate his javascript and I think it's nice when you can use asp to control what javascript you want to run.
Personaly I would rather write that code on my own using jQuery and html. I can see why you would like that approach as it abstracts the html and javascript away and you only have to specifie that you want a datepicker or whatever control you decide. But personaly I would rather write the code by hand and use inline scripting for the dynamic values. I dont realy like writing client side logic on the server side that much.
I´m all for abstractions and frameworks. But when it comes to client side, I want to write that code using client side libraries. I hope my point is clear :)