history
Wrappers around the History API for pushing and replacing entries,
navigating back/forward, and listening for popstate.
Import: @rtorcato/browser-common/history
๐ MDN: History API ยท ๐ caniuse: history
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
History.pushState | โ 5+ | โ 12+ | โ 4+ [1] | โ 5+ | โ 4+ | Widely available |
History.replaceState | โ 5+ | โ 12+ | โ 4+ [1] | โ 5+ | โ 4+ | Widely available |
- Firefox: Until Firefox 5, the passed object is serialized using JSON. Starting in Firefox 6, the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: History.pushState ยท History.replaceState
Exampleโ
import { pushState, onPopState, goBack } from '@rtorcato/browser-common/history'
pushState('/page/2', { page: 2 })
const off = onPopState((event) => {
console.log('navigated to', event.state)
})
goBack()
off()
Exportsโ
pushState(url, state?, title?)โ pushes a new history entryreplaceState(url, state?, title?)โ replaces the current history entrygoBack()โ navigates back one entrygoForward()โ navigates forward one entrygo(delta)โ navigates to a relative position in the history stackonPopState(callback)โ listens forpopstate; returns an unsubscribe function
See the API reference for full signatures.