Overview

4 Building web applications

JavaScript’s role in web development has grown from a small layer of interactivity to the foundation of many modern sites, where developers often think in terms of components instead of raw HTML and CSS. This chapter explains how a client-side web application comes to life in the browser and why understanding that process matters for performance, responsiveness, and maintainability. It frames the browser as a system that first constructs the page and then responds to user actions, with JavaScript sitting at the center of both phases.

The page-building phase begins when the browser receives HTML from the server and parses it into the DOM, a tree structure that represents the page’s content. HTML acts as the blueprint, but the DOM is the live structure the browser actually works with, and the browser may even correct invalid HTML while building it. When the parser reaches a synchronous script tag, it pauses DOM construction, executes the JavaScript, and then resumes parsing, allowing the script to read and modify the DOM before the page is fully built. The example in the chapter shows this clearly by creating elements, attaching listeners, and inserting content during script execution.

Once the page is built, the application enters the event-handling phase, where the browser waits in a single-threaded loop for queued events such as key presses, clicks, network responses, and timer callbacks. Event handlers are registered with methods like addEventListener, and because events are asynchronous, they may arrive in unpredictable order and must be processed one at a time. The chapter then shows how JSX changes the development model by blending HTML-like syntax with JavaScript, letting frameworks such as React manage DOM updates through state, virtual DOM comparison, and reconciliation. This abstraction makes UI code simpler to write, but it can also increase startup cost, so the chapter encourages careful bundle splitting, lighter frameworks when appropriate, and server-side rendering when faster initial display is important.

The lifecycle of a client-side web application starts with the user specifying a website address (or clicking a link) and ends when the user leaves the web page. It’s composed of two steps: page building and event handling.
The page-building phase starts when the browser receives the code of the page. The process of parsing HTML to build the DOM is interrupted whenever JavaScript code runs.
By the time the browser encounters the first script element, it has already created a DOM with multiple HTML elements (the nodes on the right).
An example of invalid HTML that is fixed by the browser
The DOM of the page after executing the JavaScript code contained in the script element
In the event-handling phase, all events (whether coming from the user, such as mouse clicks and key presses, or coming from the server, such as fetch events) are queued up as they occur and are processed as the single thread of execution allows.

Summary

  • The HTML code received by the browser is used as a blueprint for creating the DOM, an internal representation of the structure of a client-side web application.
  • JavaScript code can read and modify the DOM, adding interactivity to web applications.
  • DOM events can be enqueued at any time, but their handlers won’t run until the JavaScript thread is free.
  • The JSX language abstracts away the process of updating the DOM, allowing web applications to be written as a set of reusable components that render DOM trees based on their state.

FAQ

What are the two main phases of a client-side web application’s lifecycle?The lifecycle has two main phases: page building, where the browser parses HTML and constructs the DOM, and event handling, where the browser waits for and responds to events such as clicks and key presses.
How does the browser build a web page from HTML?The browser parses the HTML document one element at a time and creates a corresponding DOM tree. Each HTML element becomes a node in the DOM, which is the browser’s internal representation of the page.
What happens when the browser encounters a <script> tag during parsing?By default, the browser pauses HTML parsing, executes the JavaScript in the script tag, and then continues parsing the remaining HTML. During script execution, JavaScript can read and modify the DOM.
Why are HTML and the DOM not the same thing?HTML is the blueprint the browser uses to create the page, while the DOM is the live object tree the browser builds from that HTML. The browser may also silently fix invalid HTML while constructing the DOM.
What is the purpose of the DOMContentLoaded event?DOMContentLoaded fires when the browser has finished parsing the entire HTML document. It is commonly used to run code once the page structure is ready.
Why does JavaScript execution affect page building?Because synchronous JavaScript runs on the same single thread used for parsing HTML, it temporarily interrupts DOM construction. The browser must finish executing the script before continuing to build the page.
What is event handling in web applications?Event handling is the process of responding to browser, network, user, and timer events. The browser keeps events in an event queue and invokes the appropriate handler functions one at a time.
Why are events considered asynchronous?Events occur at unpredictable times and in unpredictable order, so their handling happens asynchronously. The browser queues events as they happen and processes them later when the main thread is available.
How do you register an event handler in modern JavaScript?You usually use addEventListener on a DOM element, such as document.addEventListener("keydown", handler). This tells the browser which function to run when the specified event occurs.
What is JSX and why is it used in frameworks like React?JSX is a syntax that combines JavaScript and HTML-like markup. It compiles to JavaScript objects and lets developers describe UI declaratively, while the framework updates the DOM automatically through reconciliation.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Secrets of the JavaScript Ninja, Third Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Secrets of the JavaScript Ninja, Third Edition ebook for free