Adding jQuery to your web pages is incredibly simple.
There are two main ways to include the jQuery library in your project.
You can download it locally, or include it from a Content Delivery Network (CDN).
You can download the jQuery library directly from jQuery.com.
Once downloaded, place the .js file inside your project folder.
You then link to it at the bottom of your HTML <body> tag using the <script> element.
<!DOCTYPE html>
<html>
<head>
<title>My First jQuery Page</title>
</head>
<body>
<h1>Hello World</h1>
<!-- Link to the downloaded file -->
<script src="jquery-3.7.1.min.js"></script>
</body>
</html>
If you don't want to download and host jQuery yourself, you can include it from a CDN.
A CDN (Content Delivery Network) is a network of servers that deliver web content to a user based on their geographic location.
Both Google and Microsoft host the jQuery library for developers to use for free.
To use the Google CDN, simply add this <script> tag to your HTML file:
<!DOCTYPE html>
<html>
<head>
<title>Google CDN jQuery</title>
</head>
<body>
<h1>Hello World</h1>
<!-- Link to Google CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</body>
</html>
Place the <script> tag just before the closing </body> tag for the best page load performance.
Once you have added the script tag, you can test if it works.
Add a simple script below your jQuery inclusion to check if it loaded successfully.
<script>
if (typeof jQuery != "undefined") {
console.log("jQuery is loaded and ready!");
}
</script>
What is the recommended way to include jQuery in your website for better performance?