Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way of having jquery includes with ASP.Net MVC

How have you guys handled working with jQuery includes

<script type="text/javascript" src="jquery.js"></script>

in Asp.Net MVC when working with partial views/view controls? Basically, I don't want to make the jquery include in the master page because I am not using jquery in all of my views, but I also want an easy way for a user control to be able to create the jquery include in the right location ('head') without creating duplicate jquery includes.

The best way would be to use a ScriptManaager, but I do not want to rely on ASP.Net Ajax for this in my application. Is there a 'lightweight' ScriptManager that I could use that would also allow the newly released intellisense for jquery?

I've created a WebControl that provides this functionality, but I don't get the intellisense support for VS2008, which I would really like.

Hope this makes sense.

like image 457
ncyankee Avatar asked Nov 08 '08 16:11

ncyankee


3 Answers

If you use JQuery in a high percentage of your pages, I'd just put it in the master page. It's only going to be downloaded once at which point it's cached on the browser and will have negligible impact on perf for rendering your page.

like image 102
Haacked Avatar answered Sep 28 '22 14:09

Haacked


Not sure how to do this with only partial views though you could do it on a page-by-page basis with existing technology.

You could create a ContentPlaceHolder in your master page inside of the head tag. Than in your user controls just put the jQuery include in the ContentPlaceHolder content area of your pages.

like image 44
Chad Moran Avatar answered Sep 28 '22 16:09

Chad Moran


Ahh trying to post this is driving me nuts cause it's being treated as part of the page... but

<script src="<%=Url.Content("~/Content/js/jquery-1.2.3.min.js") %>" type="text/javascript"></script>
like image 25
Sean Avatar answered Sep 28 '22 14:09

Sean