Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include jQuery in ASP.Net project?

Tags:

I've read that Microsoft now bundles jQuery with Visual Studio. How then do I "include" the jQuery source in my ASP.Net project?

like image 368
nw. Avatar asked Mar 02 '10 16:03

nw.


People also ask

How do I add jQuery to my project?

There are two ways to include jQuery in a project, which is to download a local copy or link to a file via Content Delivery Network (CDN). Note: A Content Delivery Network (CDN) is a system of multiple servers that deliver web content to a user based on geographical location.

HOW include jQuery in ASP NET MVC?

After the ASP.NET MVC Framework is installed a new ASP.NET MVC Web Application project should be created. Next, download jQuery, get the Packed or Minified version and place it into the Content folder of the the new web application project. Add a reference to the jQuery file put in the Content folder.


2 Answers

You can include the script file directly in your page/master page, etc using:

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

Us use a Content Delivery network like Google or Microsoft:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>  

or:

<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript"></script>      
like image 145
Nick Craver Avatar answered Oct 13 '22 23:10

Nick Craver


There are actually a few ways this can be done:

1: Download

You can download the latest version of jQuery and then include it in your page with a standard HTML script tag. This can be done within the master or an individual page.

HTML5

<script src="/scripts/jquery-2.1.0.min.js"></script> 

HTML4

<script src="/scripts/jquery-2.1.0.min.js" type="text/javascript"></script> 

2: Content Delivery Network

You can include jQuery to your site using a CDN (Content Delivery Network) such as Google's. This should help reduce page load times if the user has already visited a site using the same version from the same CDN.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 

3: NuGet Package Manager

Lastly, (my preferred) use NuGet which is shipped with Visual Studio and Visual Studio Express. This is accessed from right-clicking on your project and clicking Manage NuGet Packages.

NuGet is an open source Library Package Manager that comes as a Visual Studio extension and that makes it very easy to add, remove, and update external libraries in your Visual Studio projects and websites. Beginning ASP.NET 4.5 in C# and VB.NET, WROX, 2013

enter image description here

Once installed, a new Folder group will appear in your Solution Explorer called Scripts. Simply drag and drop the file you wish to include onto your page of choice.

This method is ideal for larger projects because if you choose to remove the files, or change versions later (though the package manager) if will automatically remove/update any reference to that file within your project.

The only downside to this approach is it does not use a CDN to host the file so page load time may be slightly slower the first time the user visits your site.

like image 35
ᴍᴀᴛᴛ ʙᴀᴋᴇʀ Avatar answered Oct 13 '22 23:10

ᴍᴀᴛᴛ ʙᴀᴋᴇʀ