Chapter 4: Building web applications
The introduction explains how JavaScript became central to web development and why developers still need to understand browser behavior.
After this chapter you will be able to
- JavaScript Abstraction Tradeoff. Explain how JavaScript shifted from enhancing HTML and CSS to supporting component-based websites, and why that shift can create performance problems.
- Two GUI lifecycle phases. Distinguish page building from event handling in the lifecycle of a client-side web application.
- HTML to DOM construction. Explain that the browser parses the HTML document received from the server to build the DOM, a tree representing the page’s contents, before the page can be displayed or interacted with.
- Script interruption of parsing. Describe the browser’s default behavior when HTML parsing reaches a script tag: it pauses parsing, executes the script, then continues; the running script can access and change the DOM. State that script-tag attributes can override this default without explaining their mechanics.
- HTML parsing into DOM nodes. Describe how the browser begins building a page’s UI by parsing received HTML one element at a time and representing each element as a DOM node.
- DOM tree relationships. Explain the parent, child, and sibling relationships between DOM nodes, including the exception for the root html node.
- HTML blueprint and DOM correction. Distinguish HTML source from the DOM the browser constructs, and describe how the browser corrects the section’s invalid paragraph-in-head example.
- Synchronous script execution timing. Trace what the browser does when it encounters a synchronous script during page building and after the script finishes.
- Script changes to the DOM. Describe how the example script changes the DOM and what kinds of DOM changes JavaScript can make.
- Event handlers and interactivity. Explain that client-side applications react to events such as clicks and key presses, and that JavaScript running during page building can register event handlers that the browser executes when those events occur.
- Single-threaded event queue. Trace how the browser processes events: it queues them in detection order, checks the head of the queue, and runs an associated handler if one exists while other events wait.
- Slow handler consequences. Explain why a long-running event handler can make a web application unresponsive.
- Event handler registration. Explain that an event handler must be registered to tell the browser which function should run for a particular event, typically using addEventListener on a DOM element.
- Events reaching another node. Describe how a keydown event fired where keyboard focus is can still trigger a handler attached to document, in contrast to DOMContentLoaded firing directly from document.
- JSX syntax and compilation. Explain that JSX lets JavaScript use HTML-like syntax to define values, but compiles to JavaScript in which tags become object representations through a runtime function.
- State-driven DOM updates. Describe how React lets a component focus on application state and a returned tree of elements rather than manually creating elements or attaching listeners. Explain that changing the example’s events array leads React to update the DOM to match that tree.
- Virtual DOM reconciliation. Trace React’s high-level update process: it constructs a virtual DOM from components’ element trees, compares its current and previous versions, and changes the page’s DOM according to the difference.
- JavaScript rendering tradeoffs. Explain why a page defined in JavaScript may appear later than one served as HTML, and describe the section’s suggested ways to reduce that initial-render cost.
Section by section
Introduction
The introduction explains how JavaScript became central to web development and why developers still need to understand browser behavior.
- JavaScript Abstraction Tradeoff: Explain how JavaScript shifted from enhancing HTML and CSS to supporting component-based websites, and why that shift can create performance problems.
Why it matters: It explains why browser fundamentals remain important even when a framework handles much of the page-building work.
Key terms: JavaScript, HTML, CSS, components, browser
4.1 The lifecycle overview
This section outlines the client-side web application lifecycle and illustrates it with a page that records script and keyboard events.
- Client-side lifecycle boundaries: Trace how a client-side web application starts with a user navigating to a page, receives the server’s response, and ends when the user leaves or closes the page.
Why it matters: These boundaries put the page-building and event-handling phases in context. - Two GUI lifecycle phases: Distinguish page building from event handling in the lifecycle of a client-side web application.
Why it matters: The two phases organize how the chapter explains a web page’s initial setup and later reactions. - Event-list example behavior: Describe how the example page records script execution, completion of HTML parsing, and key presses in an event list.
Why it matters: The visible messages make the lifecycle’s setup and reactions easier to recognize.
Key terms: client-side web application, request, response, page building, event handling, GUI, event handlers, event list, appendToEventList, DOMContentLoaded
4.2 The page-building phase
The section explains how the browser builds a page’s DOM from HTML and how a script can interrupt that process.
- HTML to DOM construction: Explain that the browser parses the HTML document received from the server to build the DOM, a tree representing the page’s contents, before the page can be displayed or interacted with.
Why it matters: This connects the HTML sent by the server to the page the browser can display and make interactive. - Script interruption of parsing: Describe the browser’s default behavior when HTML parsing reaches a script tag: it pauses parsing, executes the script, then continues; the running script can access and change the DOM. State that script-tag attributes can override this default without explaining their mechanics.
Why it matters: It explains how JavaScript execution fits into page building and why a script can affect the DOM as the page is built.
Key terms: HTML document, Document Object Model (DOM), tree, <script> tag, parsing, DOM tree, async, defer, module
4.2.1 Parsing HTML and building the DOM
This section explains how the browser turns HTML into a DOM tree, including how it can correct invalid HTML while building that tree.
- HTML parsing into DOM nodes: Describe how the browser begins building a page’s UI by parsing received HTML one element at a time and representing each element as a DOM node.
Why it matters: This connects the HTML the browser receives to the page structure it builds. - DOM tree relationships: Explain the parent, child, and sibling relationships between DOM nodes, including the exception for the root html node.
Why it matters: These relationships provide the vocabulary for describing where elements sit in a page. - HTML blueprint and DOM correction: Distinguish HTML source from the DOM the browser constructs, and describe how the browser corrects the section’s invalid paragraph-in-head example.
Why it matters: The resulting DOM does not always mirror the HTML source exactly. - DOM standard beyond HTML: Explain why HTML and DOM have separate standards, using SVG as the section’s example.
Why it matters: The DOM is a broader model than a representation used only for HTML.
Key terms: page-building phase, HTML element, DOM, node, root html node, parent, children, siblings, HTML, blueprint
4.2.2 Executing JavaScript code
This section explains how a synchronous script interrupts page building, changes the DOM, and leaves global state available when HTML processing resumes.
- Synchronous script execution timing: Trace what the browser does when it encounters a synchronous script during page building and after the script finishes.
Why it matters: This order explains when a script runs relative to the DOM being built and events being handled. - Script changes to the DOM: Describe how the example script changes the DOM and what kinds of DOM changes JavaScript can make.
Why it matters: It shows that executing a script can change the page’s DOM before any event fires. - Persistent global script state: Explain why global variables created by one script element are normally available to another script element.
Why it matters: A pause between script elements does not normally erase the application’s global variables.
Key terms: synchronous script node, page-building phase, DOM construction, event handling, document, querySelector, event-list, appendToEventList, li element, DOM nodes
4.3 Event handling
Introduces event handlers as the way client-side JavaScript makes web applications interactive.
- Event handlers and interactivity: Explain that client-side applications react to events such as clicks and key presses, and that JavaScript running during page building can register event handlers that the browser executes when those events occur.
Why it matters: This connects JavaScript's page-building work to how an application responds to users.
Key terms: events, event listeners, handlers, page-building phase
4.3.1 Event-handling overview
This section explains how the browser queues and handles events one at a time, why slow handlers make pages unresponsive, and why event handling is asynchronous.
- Single-threaded event queue: Trace how the browser processes events: it queues them in detection order, checks the head of the queue, and runs an associated handler if one exists while other events wait.
Why it matters: This explains why an event can occur before its handler gets a turn to run. - Slow handler consequences: Explain why a long-running event handler can make a web application unresponsive.
Why it matters: It connects the one-at-a-time model to a delay users can notice. - Independent event queuing: Distinguish the browser’s detection and queuing of events from the thread that handles them, using the repeated-click example.
Why it matters: It explains why a frozen page may react to clicks made during the freeze. - Asynchronous event sources: Describe why event handling is asynchronous and identify the kinds of events this section gives as examples.
Why it matters: It explains why much of a web application’s JavaScript runs in response to things whose timing it does not control.
Key terms: single-threaded execution model, event queue, event-handler function, event handlers, unresponsive web applications, event-handling phases, asynchronous, browser events, network events, user events
4.3.2 Registering event handlers
Explains how event handlers are registered and why an event handler on the document can receive a keyboard event fired elsewhere.
- Event handler registration: Explain that an event handler must be registered to tell the browser which function should run for a particular event, typically using addEventListener on a DOM element.
Why it matters: Registration connects an event to the function you want the browser to run. - Events reaching another node: Describe how a keydown event fired where keyboard focus is can still trigger a handler attached to document, in contrast to DOMContentLoaded firing directly from document.
Why it matters: It explains why a handler on document can respond to typing in an input.
Key terms: event handler, event-handler registration, addEventListener, DOM element, DOMContentLoaded, keydown, document, keyboard focus, bubbling
4.4 Writing interactive HTML with JSX
This section explains how JSX and React let developers describe interactive pages as component output, while showing how React updates the DOM and why JavaScript-rendered pages can be slower to display.
- JSX syntax and compilation: Explain that JSX lets JavaScript use HTML-like syntax to define values, but compiles to JavaScript in which tags become object representations through a runtime function.
Why it matters: It clarifies what the HTML-like code in a JSX component becomes before it runs. - State-driven DOM updates: Describe how React lets a component focus on application state and a returned tree of elements rather than manually creating elements or attaching listeners. Explain that changing the example’s events array leads React to update the DOM to match that tree.
Why it matters: This is the practical difference between writing the example with React and manipulating the DOM directly. - Virtual DOM reconciliation: Trace React’s high-level update process: it constructs a virtual DOM from components’ element trees, compares its current and previous versions, and changes the page’s DOM according to the difference.
Why it matters: It explains, at a high level, how React turns changed component output into DOM updates. - JavaScript rendering tradeoffs: Explain why a page defined in JavaScript may appear later than one served as HTML, and describe the section’s suggested ways to reduce that initial-render cost.
Why it matters: The convenience of JSX and React has an initial-display cost worth considering when building a page.
Key terms: JSX, JSX compiler, runtime library, _jsx, state, events array, component, tree of elements, DOM, virtual DOM
4.5 Summary
Where this is used later
- Promise timing: Of microtasks and macrotasks (chapter 8) · The earlier event-handling overview.
- The art of code splitting (chapter 13) · The recommendation to defer code unnecessary for the initial render motivates code splitting.
Concepts explained in this chapter
- handlerA function invoked in response to an event or request to perform associated application logic.
- event handlingEvent handling is the process of receiving events and invoking associated handler functions to respond to them.
- JSXJSX is a syntax extension that lets developers write XML-like markup within JavaScript code.
- Document Object ModelThe DOM is the programming interface representing a web document as a tree of objects that scripts can inspect and modify.
- HTMLThe standard markup language for structuring documents and content on the web.
- single-threaded execution modelSingle-threaded execution means JavaScript code executes in one thread of control at a time.
- ReactReact is an open-source JavaScript library for building user interfaces from declarative components.
- virtual DOMA virtual DOM is an in-memory tree representation of a user interface that can be compared with the rendered document to determine updates.
- JavaScriptJavaScript is a high-level programming language standardized as ECMAScript and widely used for web and general-purpose software development.
- event loopAn event loop is a mechanism that repeatedly processes queued tasks and callbacks to support asynchronous, event-driven execution.
- server-side renderingA rendering technique in which HTML for a web page is generated on the server before being delivered to the browser.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
