Where to place jQuery code on an ASP.NET MVC view page?

I just started learning jQuery + ASP.NET MVC. No matter what tutorials I read about jQuery, they assume that all jQuery code should be in the main element.

Now in the ASP.NET MVC project, I have one main page that is responsible for the head element. On other view pages, I get a content holder that draws a body element.

Now I'm confused. Should I ignore the advice on saving jQuery in the head element, or is there a way to write different jQuery code on each view page?

+4
source share
4 answers

On the main page, you can add more content holders that your content pages can fill with their own jQuery

So, at the head of your main page, do something like:

<asp:ContentPlaceHolder ID="Javascript" runat="server" /> 

Then on your screen browse the pages

 <asp:Content ID="Content1" ContentPlaceHolderID="Javsacript" runat="server"> //js here </asp:Content> 

However, you might consider including your javsascript in separate JS files and including them in order to relate your problems a bit.

+8
source

Save js in external .js files. Thus, they are cached.

+9
source
  • Refactoring shared files for jQuery plugins
  • Put related things and unrelated things in your own js files.
  • Run jslint in all js files you wrote.
  • You have a bunch of <script src=... -tags at design time (your wizard)
  • Before deploying to a test server, minimize and concentrate all js files on a large one so that it is cached and you do not receive as many HTTP calls.
  • If one of your js files is huge and is needed only for a specific page, exclude only this file from the large one.
+2
source

I would put them outside and use the <script type="text/javascript" src='<%= Url.Content(~/scriptlocation) %> /> to reference them

0
source

All Articles