4

I have just started to learn jQuery + ASP.NET MVC. Whatever tutorials I have read about jQuery, they suggest that all jQuery code should be in head element.

Now in the ASP.NET MVC project, I have one master page which is responsible for head element. In other view pages, I get content place holder which draws in body element.

I am confused now. Should I ignore the advice of keeping jQuery in head element or there is some way to write different jQuery code in each view page?

4 Answers 4

9

Keep your js in external .js files. That way they get cached.

Sign up to request clarification or add additional context in comments.

3 Comments

Should I write one JavaScript file for each page? I will endup with too many js files?
You can start it off like that then refactor common functionality . The key thing is to have a minify/merge build action that basically gives you one script file for the site. The less individual downloads the better.
I would do a little bit of upfront design. Surely the pages will have common functionality running across them as in Tabs/Menu etc
8

You can include more content place holders in your master page, which your content pages can then fill with thier own JQuery

So in the head of your master page make something like:

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

Then in your view pages

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

That said, you should maybe consider including your javsascript in seperate JS files and include them, to seperate your concerns a bit.

Comments

2
  • Refactor common stuff to jQuery plugins
  • Put related things together and unrelated things in their own js-files
  • Run jslint on all js-files you've written
  • Have a bunch of <script src=...-tags during development (in your master)
  • Before deploying to test server, minify and concatinate all js-files into a big one, so that it gets cached and you don't get that many http calls.
  • If one of your js-files is huge and only needed for a specific page, exclude just that file from the big one.

4 Comments

Makes sense. What does "Run jsInit on all js-files..." mean?
One more point: If many view pages have $(document).ready implemented, concatenating the js files will result in all those functions fired. How to solve this problem (or is it really a problem)?
You really need an init function per page. Look here for inspiration paulirish.com/2009/…
jslint is a code verifier: jslint.com multiple $(document).ready: No, I don't think so. Often you have behaviour that goes beyond one single page, and for the behaviour that is specific to a specific page, you could give the body element for that page a class and check if it exists or not.
0

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

3 Comments

The one who has downvoted it, please can you write a reason of doing so?
I am not the one who downvoted you, but there is an obvious mistake. JavaScript files are included via the <script> tag not <link>.
@korchev, whilst this is true, one of the great things about stackoverflow is that you can edit other people's posts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.