liveBook · Manning

Event handling

As explained in Secrets of the JavaScript Ninja, Third Edition

Event handling is the process of receiving events and invoking associated handler functions to respond to them.

Event handling gives a browser-based interface interactivity: code registers listener functions, which the browser invokes when matching events occur. section 4.3

Events are processed in turn:

  • The browser queues detected events in order. It checks the queue and runs the associated handler, if one exists; other events wait until that handler finishes. section 4.3.1
  • Because only one event is handled at a time, lengthy handlers can make an application unresponsive. section 4.3.1

A page that reports events

A small interface appends messages to a list when DOMContentLoaded fires and whenever a keydown event occurs. Typing “asdf” produces one message for each key press. section 4.1

Figure 4.6 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.

The figure traces how detected events enter a queue and are handled as the single thread allows. section 4.3.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Traces event processing through a queue and one-at-a-time handler execution, while deferring detailed event-loop effects.
Examples
Demonstrates DOMContentLoaded and keydown handlers that append event messages to a list.
What you can do after
Apply it via browser event listeners to make a page respond to user actions.
How it connects
Connects event handling with the DOM, event listeners, the event queue, and the event loop.

Also explained in

Compare how 3 books explain event handling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.