Course contentsShow
← Web Development
Lesson 1091 of 2,98219. Keyboard Navigation and FocusFree lesson

Route Change Focus Management

Managing focus during SPA route transitions to maintain context and announce page changes to screen readers.

Route Change Focus Management

You'll learn how to keep keyboard users and screen reader users oriented when navigating between pages in a single-page application.

The Problem with Route Changes

In traditional multi-page websites, when you click a link to a new page, the browser automatically resets focus to the top of the document and screen readers announce the new page. But in single-page applications (SPAs), JavaScript swaps content without a full page reload—the browser doesn't know a "new page" has loaded. Keyboard users and screen reader users can be left confused, staring at the same spot in the DOM while the content around them has completely changed.

How to Manage Focus on Route Changes

When your application navigates to a new route, you need to manually move focus to signal that the context has changed. The most common approach:

  1. Move focus to the main content heading of the new view
  2. Make the heading focusable using tabindex="-1" (since headings aren't naturally focusable)
  3. Call .focus() programmatically after the route transition completes

This accomplishes two things: keyboard users know where they are in the page structure, and screen readers announce the new heading content, effectively "reading" the new page title.

The Experience

Think of it like a librarian guiding a blindfolded visitor. When you walk them to a new section, you don't just teleport them there silently—you tell them "You're now in the Science Fiction section" so they have context. Route change focus management is that announcement.

Most modern routing libraries provide lifecycle hooks specifically for this purpose—run your focus management code after the new content renders but before the user tries to interact.

Key Takeaway: Always move focus to a meaningful location (typically the main heading) after SPA route changes to maintain context for keyboard and screen reader users.