Description#
Implement the History API (window.history) per the WHATWG HTML spec. This enables single-page applications to manage navigation state without full page reloads.
Requirements#
Properties#
history.length— number of entries in the session historyhistory.state— the state object for the current entry (ornull)history.scrollRestoration—autoormanual
Methods#
history.pushState(state, title, url)— push a new entry without page reload. URL must be same-origin. State is structured-cloned.history.replaceState(state, title, url)— replace the current entry without page reload. Same constraints as pushState.history.back()— equivalent tohistory.go(-1)history.forward()— equivalent tohistory.go(1)history.go(delta)— traverse by delta steps.history.go(0)reloads.
Events#
popstateevent fires on the window when the active history entry changes due to traversal (back/forward/go), NOT when pushState/replaceState is called- Event has a
stateproperty containing the state object of the new entry popstatefires after the entry is activated
Constraints#
pushState/replaceStateURL must be same-origin as current document- State object must be serializable (structured clone)
- Rapid pushState calls should be rate-limited to prevent abuse
Acceptance Criteria#
-
pushStateadds entry and updates URL bar without page reload -
replaceStateupdates current entry without adding new one -
back()/forward()/go()traverse the history -
popstateevent fires with correct state on traversal -
history.lengthreflects actual entry count - Cross-origin URL in pushState throws SecurityError
- Unit tests for all History API methods and popstate event