let h = new History() is illegal. So I made my own. Same interface; push and restore anything that represents your state. The
scrollRestoration property is undefined, since that’s only relevant for browser history.
history.js: the code.
history.test.html: simple test suite.
See the actual History documentation for the methods and properties.
let h = new History (initialState); h.pushState(newState); let s = h.state; // the current state s = h.back(); // the previous state s = h.forward(); // return to the state of 2 lines ago.
The only difference is that the
restoreState return the current state.
There are two read-only Boolean properties that are useful and I don’t know why they aren’t included:
h.atStart === true // when at the beginning of the history list, when h.back() leaves the state unchanged h.atEnd === true // when at the end of the history list, when h.forward() leaves the state unchanged
History but that should never be a problem, since it is impossible to instantiate the original
let h = new History() throws an Illegal Constructor error. The original
window.history is unchanged.