HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

jQuery Installation

To start using jQuery, you first need to include the jQuery library in your HTML project. There are two main ways to do this: you can either use a **CDN** (linked from a external server) or **download** the file and host it yourself.


Method 1: Using a CDN (Recommended)

A **Content Delivery Network (CDN)** is a system of distributed servers that deliver web content to users based on their geographic location. Using a CDN is the fastest and easiest way to get started.

Why use a CDN?

  • Speed: CDNs deliver the file from the server closest to the user.
  • Caching: If a user has already visited a site that uses the same jQuery CDN, it’s already cached in their browser and won’t need to be re-downloaded.
  • Reliability: Large CDNs have massive uptime and can handle heavy traffic easily.

Simply copy and paste one of the following script tags into your HTML header or before the closing </body> tag:

Google CDN: Recommended for most users.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
Microsoft CDN: A great alternative.
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.7.1.min.js"></script>
Official jQuery CDN: Always has the latest version.
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

Method 2: Downloading jQuery Locally

If you prefer to host jQuery on your own server (useful for offline development or restricted internal networks), you can download it from **jquery.com**.

Production vs. Development:
  • Compressed (minified): Use this for live websites. It's smaller and loads faster.
  • Uncompressed: Use this if you need to read or debug the jQuery source code itself.

Once downloaded, place the file in your project folder (e.g., in a `js/` folder) and link to it like this:

<!-- Linking to a local file -->
<script src="js/jquery-3.7.1.min.js"></script>

Where to Place the Script Tag?

You can place the <script> tag in two places, but there is a clear better choice for modern websites:

Location Pros/Cons
Inside <head> Older practice. Ensures jQuery is ready before the page renders, but can slow down initial page loading.
Before </body> Recommended. Allows the HTML and CSS to load first, making the site feel faster to the user.
Pro-Tip: Always place the jQuery library tag above your custom script tags. Your scripts cannot use jQuery if it hasn't loaded yet!

How to Verify if jQuery is Working?

After adding the script tag, you can test if jQuery is loaded correctly by running a small piece of code in your console or within a <script> tag:

$(document).ready(function() {
    console.log("jQuery is correctly installed and running!");
    alert("jQuery works!");
});

Key Points to Remember

  • Use a CDN for better performance and speed through caching.
  • The minified (.min.js) version is best for production environments.
  • Always include jQuery before your own custom JavaScript files.
  • You only need to include jQuery once per page.
  • Version 3.x is the current standard; versions 1.x and 2.x are used only for very old browser support.