Welcome to the JQuery CDN Usage tutorial! In this lesson, we'll dive into using the Content Delivery Network (CDN) to effortlessly integrate JQuery into your web projects.
A CDN (Content Delivery Network) is a large distributed system of servers deployed in multiple regions around the world. Its primary purpose is to deliver content, such as HTML pages, stylesheets, scripts, images, and videos, with high availability and high performance.
Using a CDN for JQuery offers several benefits:
To use a CDN for JQuery, we'll use the jQuery CDN hosted by jQuery itself.
First, let's include the JQuery library in our HTML file by adding the following script tag at the end of the <head> section:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>š Note: Be sure to replace 3.6.0 with the latest version of JQuery when you read this.
Now that JQuery is included via a CDN, we can write JavaScript code that utilizes the JQuery library.
Let's create a simple JQuery script that changes the text of an HTML element when the page loads:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JQuery CDN Usage Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1 id="title">Welcome to CodeYourCraft!</h1>
<script>
$(document).ready(function () {
$('#title').text('Hello, World!');
});
</script>
</body>
</html>In this example, we've added an h1 element with an id of title. We've also included the JQuery library via the CDN in the head section. Inside the script tag, we've used the $(document).ready() function to ensure our script waits for the DOM to load before executing the code. Finally, we've used the JQuery text() method to change the text of the title element to "Hello, World!".
Now that we have the basics down, let's dive into a more advanced example that demonstrates how to use JQuery to create a dynamic, filterable portfolio gallery:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JQuery CDN Usage Example - Portfolio Gallery</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Portfolio</h1>
<input type="text" id="search" placeholder="Search Projects">
</header>
<div id="projects"></div>
<script src="script.js"></script>
</body>
</html>In this example, we've created a simple HTML structure with a search bar and a container for our projects. We've also included our CSS and JavaScript files.
Our JavaScript file (script.js) will contain the JQuery code to create the dynamic, filterable portfolio gallery.
$(document).ready(function () {
// Load projects from JSON file
$.getJSON('projects.json', function (data) {
var projects = data;
// Iterate through projects and create HTML elements
projects.forEach(function (project) {
var projectElement = `
<div class="project">
<img src="${project.image}" alt="${project.title}">
<h3>${project.title}</h3>
<p>${project.description}</p>
</div>
`;
$('#projects').append(projectElement);
});
// Filter projects based on user input
$('#search').on('input', function () {
var userInput = $(this).val().toLowerCase();
$('#projects .project').hide();
$('#projects .project').filter(function () {
return $(this).text().toLowerCase().includes(userInput);
}).show();
});
});
});In this example, we've used JQuery's getJSON() method to load a JSON file containing our projects. We then iterate through the projects and create HTML elements for each one. Additionally, we've added an event listener to the search bar that filters the portfolio projects based on user input.
š Note: For the purpose of this example, we've assumed that the projects.json file contains an array of objects, each representing a project with properties like title, description, and image.
What is the purpose of using a CDN for JQuery?
That's it for our JQuery CDN Usage tutorial! You now have the knowledge to easily integrate JQuery into your web projects using a CDN. Happy coding! š