JavaScript Interview Question #170

What is the History API?

Performance, Browser APIs, Rendering & Modern Web Architecture Senior Advanced

Quick Interview Answer

The History API allows browser applications to interact with session history. pushState adds a history entry without performing a full page navigation, while replaceState replaces the current entry.

Detailed Explanation

The History API allows browser applications to interact with session history. pushState adds a history entry without performing a full page navigation, while replaceState replaces the current entry.

The popstate event helps applications respond when the user navigates through history. Client-side routers use these APIs, but the server must still be configured to serve appropriate content for directly requested application routes.

Code Example

history.pushState(
    { page: "products" },
    "",
    "/products"
);

window.addEventListener("popstate", event => {
    console.log("History state:", event.state);
});