Page Content

Tutorials

Downloading and Setting up jQuery With Code Example

Downloading and Setting up jQuery

Downloading and Setting up jQuery
Downloading and Setting up jQuery

Setting up and downloading When creating interactive web front ends, jQuery is an essential first step. jQuery is a quick and easy JavaScript library made to make Ajax interactions, event handling, HTML page traversal, and animation simpler for quick web development. Managing browser incompatibilities, which may frequently be challenging and time-consuming, is one of the many hard JavaScript development tasks it streamlines.

Downloading jQuery

You can get the jQuery library for your web projects in two main ways:

Local Installation: The jQuery library file can be downloaded to your computer and included to your HTML code.

  • The official jQuery website for the latest stable versions.
  • Minified and uncompressed downloads are common. The “min” in jquery-1.4.min.js removes useless whitespace and comments to reduce file size. Its smaller size makes the minified version ideal for production.

CDN Based Version: The jQuery library can be included straight from a content delivery network (CDN).

  • CDN services for jQuery are provided by well-known corporations like Google and Microsoft. The CDN is also offered by the jQuery project itself.
  • Increased dependability and quicker loading times because to server dispersion and caching (users may already have the file cached if they have visited other websites with the same CDN-hosted jQuery version) are two benefits of employing a CDN.
  • This indicates that the jQuery file is accessible from strong, fast servers located all over the world. Although local copies are useful for development (since they let you work offline), CDNs are frequently chosen for live sites.

Setting Up a Local Testing Environment

Establishing a structured testing directory on your PC is useful before you start working with jQuery locally:

Create a main directory:To organise your files, you must create a main directory, also known as a testing directory or project directory, when starting a jQuery project. This keeps all web project information in one location. You may name this primary directory webtest and put it on your C:/ drive or desktop on a PC or Mac. Subdirectories are recommended for categorising assets in this webtest directory. JavaScript, stylesheets, and pictures are common subdirectories.

Create subdirectories: Subdirectories are essential for organising web project, especially when using jQuery. This organisation keeps your development environment clear and efficient. Practically, you should construct a primary project directory (like webtest) on your PC and numerous specialised subdirectories within it.

Place the jQuery file: Your HTML document must have access to the jQuery library file to use it. Local installation or a Content Delivery Network are the main methods. A CDN-based version hosts the jQuery file on strong, low-latency servers distributed globally by Google, Microsoft, or the jQuery team. This strategy saves server bandwidth and speeds up loading times via server dispersal and caching.

Including jQuery in an HTML Document

Once you have the jQuery file, you need to reference it in your HTML document using a <script> tag. This tag tells the browser where to find the jQuery library.

Placement of the Script Tag: Your unique JavaScript code must appear after the <script> tag for the jQuery library.It guarantees that when your scripts use the jQuery framework, it is completely loaded and available.

Using the src Attribute: The <script> tag has a src attribute that points to the jQuery file.

  • When installing locally, the src will point to your js directory <script type=”text/javascript” src=”js/jquery-1.4.min.js”></script>. An example of a CDN-based version would be <script type=”text/javascript”.
  • src=”http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”></script>, where the src will be the URL supplied by the CDN (for example, from Google’s CDN).

Executing Code After DOM Is Ready:

A key component of jQuery is making sure your code executes at the appropriate moment. JavaScript code might cause issues if it attempts to modify HTML components before they are completely loaded and built in the Document Object Model (DOM). In order to fix this, the $(document).ready() method makes sure that the code inside it only runs once the web page has completely loaded and the DOM has been properly formed. By filling in the gaps in the pure DOM notion and compensating for numerous browser-dependent particularities, jQuery code becomes dependable across browsers.

The basic syntax for $(document).ready() is:

Syntax:

$(document).ready(function() {
  // Your jQuery code goes here
});

There is also a shorthand version that is commonly used:

$(function() {
  // Your jQuery code goes here
});

When calling a jQuery function or accessing an element of the webpage, the $ symbol in jQuery code serves as a shorthand notation.

Code Example: Getting Started with jQuery

This thorough HTML example shows how to set up and use a basic jQuery effect, showing “Hello, World!” when the DOM is ready

<!DOCTYPE html>
<html>
<head>
  <title>My First jQuery Page</title>
  <!-- Load jQuery from a local file or use a CDN for reliability -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script>
    $(document).ready(function() {
      // Append "Hello, World!" after the paragraph
      $("body").append("<p>Hello, World!</p>");
    });
  </script>
</head>
<body>
  <h1>Page Content</h1>
  <p>This is some initial text.</p>
</body>
</html>

Output:

Page Content
This is some initial text.
Hello, World!

In this example:

  • Line 7’s script> tag, assuming jquery-1.8.min, refers to the locally cached jQuery library.A js subfolder containing this HTML file contains JavaScript.
  • The $(document) is at line 9-14 of the custom script block.ready() function.
  • The fact that document.write(“Hello, World!”); only runs inside this function after the HTML document structure has completely loaded shows that jQuery is configured appropriately and that your custom code is executed when it should.

These instructions will help you download and install jQuery efficiently, readying your website for dynamic and interactive additions.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index