I have an Asp.net MVC project that modestly uses jQuery scripts. My views also display partial views in them, either returned using RenderPartial
or RenderAction
helper methods. Partial views are usually used to encapsulate some common display but they may as well have some client-side script functionality. My javascripts have the same names as views to which they are related.
So if I have a partial view UserInformation.ascx
I have a matching UserInformation.js
file. Most of these script files register functionality for DOM ready event so they won't be executed immediately.
The problem is, that these scripts are added to parent views. So if a certain view uses my partial view it also needs to add its matching script file. So if someone forgets to add the script to main view, my partial view functionality won't work.
In terms of browser execution and speed (or any other issues) is it better to include all scripts in XHTML head element or is it irrelevant if I'd reference these scripts inside my partial views? This way I could avoid human error of forgetting to add the script to the view? I could get a problem of referencing the same script file multiple times though. Especially if the same partial is used several times on the same view.
Are there any differences when we reference scripts inside HEAD or BODY elements?
Are there any doctype related issues (I'm using XHTML Transitional)?
What is the most common pattern?
It would be nice to have some sort of script registration functionality in Asp.net MVC that would prevent me from loading the same script file multiple times...