Installing and Including jQuery
Before using jQuery, we need to include it in our project. jQuery provides multiple ways to include it, suitable for different development environments and requirements.
Overview of Inclusion Methods
jQuery provides the following inclusion methods:
- CDN: The simplest and fastest method, suitable for rapid prototyping
- Download File: Suitable for offline environments or when complete control of files is needed
- NPM Installation: Suitable for modern frontend projects, convenient for version management and build optimization
- Package Managers: Such as Yarn, Bower
Method 1: CDN (Recommended for Beginners)
CDN (Content Delivery Network) is the fastest and simplest way to include jQuery. Just add a script tag to your HTML file.
Common CDN Providers
Advantages of CDN
- Fast Loading: CDN servers distributed worldwide provide faster content delivery
- Caching Benefits: Many users may have already cached jQuery from other websites
- Reduced Server Load: Offloads bandwidth from your server
Method 2: Download Local File
1. Download the File
Visit the jQuery official website, click the "Download" button, and select the version you need.
2. Copy to Your Project
Copy the downloaded jQuery file to your project directory, for example:
3. Include in HTML
Add the following code to your HTML file:
Method 3: NPM Installation
For projects using modern frontend build tools (like Webpack, Vite), NPM installation is recommended.
1. Install jQuery
Open terminal in your project root directory and run:
2. Include in Your Project
The inclusion method varies slightly depending on your framework and build tool:
Include in JavaScript file:
Include in CommonJS environment:
Method 4: Using Package Managers
Install with Yarn:
Install with Bower:
jQuery Version Selection
jQuery has multiple versions to choose from:
1. jQuery 3.x (Recommended)
- Latest version
- Does not support IE 6/7/8
- Performance optimized
- Uses strict mode
2. jQuery 2.x
- Does not support IE 6/7/8
- Smaller file size
- Improved performance
3. jQuery 1.x
- Supports IE 6/7/8
- Larger file size
- Suitable for projects requiring old browser compatibility
4. jQuery Compat 3.x
- Compatible version containing deprecated APIs
- Suitable for projects migrating from older versions
Verifying Installation
Regardless of which method you use to include jQuery, you can verify successful installation with:
If the page displays "jQuery has been successfully loaded and is working!" in green text, jQuery has been successfully installed and is ready to use.
Multiple Version Coexistence
In some cases, you may need to use multiple versions of jQuery in the same project:
Important Notes
- Inclusion Order: Ensure jQuery is included before any scripts that use it
- Network Connection: CDN method requires a stable network connection
- Version Compatibility: Ensure the jQuery version is compatible with your project requirements
- File Path: When using local inclusion, ensure the file path is correct
- Browser Cache: If you update the jQuery version, you may need to clear browser cache
- Security: When using CDN, it's recommended to specify integrity check
With these methods, you can successfully include jQuery in your project and start using it to simplify your JavaScript development work.