I’m just getting started with ASP.NET MVC. I’m going to be using JQuery on the website I’m making, but I’m not really sure about one thing: where would JQuery code be placed?
This concerns two things:
- Where do I import the JQuery JavaScript file? (I’ve been thinking that the Master page would be a good place to do this; am I right, or do I have to import it in each view?)
- Should all my JQuery code be referenced from the Master page (i.e., I store my code that uses JQuery in a separate
.jsfile and reference it in a<script>tag)?
Thanks in advance.
Anything that you will use in every page put a import in your master. If its something that you will only use on a smaller scale, put the import in the view that will use it.
The key is to not load unnecessary bytes when they won’t even be used. I have a universal js file that contains anything that will be used as more common functions across the board imported in my master. Then on a page by page basis, I have only the js imports that I need for that page.
If you put every js import in your master that means it will load that js on every page, when maybe half of them aren’t even used for that particular page. That can have a big impact on page load times.
I suggest also using something that can compress the js to a minified version for production. Telerik has their Script Registrar and it’s a really nice tool as well.