Page Content

Tutorials

What are the Basic Web Page Structure in jQuery With Example

Basic Web Page Structure

Basic Web Page Structure
Basic Web Page Structure

Using jQuery to create dynamic web front ends involves knowledge of HTML page structure and jQuery code integration best practices. These ensure that your scripts run consistently and connect with web page components.

Fundamental HTML Structure

HTML files have three basic parts: <html>, <head>, and <body>. This layout makes visible text, and metadata easier to differentiate.

<html> Elements:

  • This is the root element of every HTML page.
  • It tells the web browser that everything contained within its opening (<html>) and closing (</html>) tags should be interpreted as an HTML document.

<head> Element: Despite being hidden from users, the element contains necessary links and metadata for the page’s functionality and formatting.

Common elements found within the <head> include:

  • <title> Defines the title of the web page, which appears in the browser’s tab or window title bar.
  • <meta> tags Provide various types of metadata, such as character set (charset) and viewport settings for mobile responsiveness.
  • <link> tagsLink to external Cascading Style Sheets (CSS) files, which control the visual presentation of the page.
  • <script> tags Crucially, this is where references to JavaScript libraries, including jQuery, and custom JavaScript code are typically placed.

<body> Element:

  • Page visitors interact with visible content in the element.
  • Text, photos, forms, videos, and other interactive components are all included in this.
  • A Document Object Model (DOM) tree structure is formed by the nesting of HTML components inside the . They may be siblings, parents, or children.

Placement of jQuery Code

For jQuery to function correctly, the jQuery library file must be loaded into the HTML document before any custom JavaScript code that depends on it. This is typically achieved by placing a <script> tag referencing the jQuery library file in the <head> section of your HTML document, followed by another <script> tag for your custom JavaScript file.

There are two primary ways to include the jQuery library:

Local Installation: You can download the jQuery library file from the official jQuery website and store it in a local directory within your project, such as a lib or js subdirectory. Your HTML file then references this local path.

CDN Based Version: You can link to the jQuery library hosted on a Content Delivery Network (CDN) provided by entities like Google, Microsoft, or jQuery itself. CDNs offer advantages like faster loading times due to distributed servers and caching.

Once the jQuery library is referenced, your custom jQuery code should typically be placed within a $(document).ready() handler. This method is crucial because it ensures that your code only executes once the entire Document Object Model (DOM) has been fully loaded and parsed by the browser.

Why $(document).ready() is Essential:

Prevents Errors from Premature Execution: Web browsers parse HTML documents from top to bottom. If your jQuery code tries to select or manipulate an HTML element before that element has been created in the DOM, it will result in an error or simply have no effect. $(document).ready() solves this by delaying execution until the DOM is ready for interaction.

Cross-Browser Compatibility: Different browsers may process or “parse” the web page at different speeds or in slightly different ways. $(document).ready() provides a reliable and consistent cross-browser solution for handling the “DOM ready” state, compensating for these particularities and supplementing missing functions of the pure DOM concept. This means your code works reliably across officially supported browsers.

Improved User Experience: Unlike the native window.onload event handler, which waits for all page (including images, iframes, etc.) to be fully loaded, $(document).ready() fires as soon as the HTML structure is available. This allows your interactive elements to become functional earlier, providing a snappier user experience, even if larger assets are still downloading in the background. While window.onload can also be used, it is often seen as unreliable due to inconsistent implementation across browsers and its delay until are loaded.

Non-Obtrusive Scripting: $(document).ready() promotes the principle of non-obtrusive scripting, which advocates for separating JavaScript behavior from HTML markup. Instead of embedding event handlers directly in HTML (e.g., <button onclick=”myFunction()”>), jQuery allows you to define all behaviors purely in JavaScript within a single, organized location.

Shorthand Syntax: jQuery provides a convenient shorthand for $(document).ready(function(){…}), which is $(function(){…}). While both achieve the same result, the longer version is often preferred for its descriptiveness and readability.

Here’s a basic example of a web page structure with jQuery code placement:

Example:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>My First jQuery Page</title>
  <!-- Link to external CSS (optional, for styling) -->
  <link rel="stylesheet" type="text/css" href="styles.css">
  <!-- Link to jQuery Library (local or CDN) -->
  <!-- Option 1: Local file -->
  <!-- <script src="lib/jquery-1.8.min.js"></script> -->
  <!-- Option 2: Recommended CDN version -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <!-- Your jQuery Script -->
  <script>
    $(document).ready(function() {
      $("#myButton").click(function() {
        $("#output").html("You clicked the button! Here's a new message.");
        $("#output").addClass("highlight");
      });
    });
  </script>
  <!-- Optional CSS Styling -->
  <style>
    .highlight {
      color: white;
      background-color: green;
      padding: 10px;
      border-radius: 5px;
    }
  </style>
</head>
<body>
  <h1>Welcome to jQuery</h1>
  <p id="output">Click the button below to see a change.</p>
  <button id="myButton">Click Me</button>
</body>
</html>

Output:

Welcome to jQuery
You clicked the button! Here's a new message.

Click Me

In this example:

  • The <html>, <head>, and <body> tags define the fundamental structure of the web page.
  • The jQuery library is loaded first in the <head>.
  • Your custom script block contains $(document).ready().
  • Inside $(document).ready(), jQuery selectors ($(“#myButton”), $(“#output”)) are used to find elements and jQuery methods (.click(), .html(), .addClass()) are applied to them to add interactivity and change content or appearance dynamically.

This organized approach ensures that jQuery code interacts with the web page in a stable, efficient, and maintainable manner, regardless of browser specifics or content loading order.

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