Navigation - Web APIs | MDN
Instance properties
Inherits properties from its parent, EventTarget.
activationRead only-
Returns a
NavigationActivationobject containing information about the most recent cross-document navigation, which "activated" this Document. canGoBackRead only-
Returns
trueif it is possible to navigate backwards in the navigation history (i.e., thecurrentEntryis not the first one in the history entry list), andfalseif it is not. canGoForwardRead only-
Returns
trueif it is possible to navigate forwards in the navigation history (i.e., thecurrentEntryis not the last one in the history entry list), andfalseif it is not. currentEntryRead only-
Returns a
NavigationHistoryEntryobject representing the location the user is currently navigated to right now. transitionRead only-
Returns a
NavigationTransitionobject representing the status of an in-progress navigation, which can be used to track it. Returnsnullif no navigation is currently in progress.
Instance methods
Inherits methods from its parent, EventTarget.
back()-
Navigates backwards by one entry in the navigation history.
entries()-
Returns an array of
NavigationHistoryEntryobjects representing all existing history entries. forward()-
Navigates forwards by one entry in the navigation history.
-
Navigates to a specific URL, updating any provided state in the history entries list.
reload()-
Reloads the current URL, updating any provided state in the history entries list.
traverseTo()-
Navigates to a specific
NavigationHistoryEntryidentified bykey. updateCurrentEntry()-
Updates the state of the
currentEntry; used in cases where the state change will be independent from a navigation or reload.
Events
Inherits events from its parent, EventTarget.
currententrychange-
Fired when the
Navigation.currentEntryhas changed. -
Fired when any type of navigation is initiated, allowing you to intercept as required.
-
Fired when a navigation fails.
-
Fired when a successful navigation has finished.
Examples
Moving forwards and backwards in the history
js
async function backHandler() {
if (navigation.canGoBack) {
await navigation.back().finished;
// Handle any required clean-up after
// navigation has finished
} else {
displayBanner("You are on the first page");
}
}
async function forwardHandler() {
if (navigation.canGoForward) {
await navigation.forward().finished;
// Handle any required clean-up after
// navigation has finished
} else {
displayBanner("You are on the last page");
}
}
Traversing to a specific history entry
js
// On JS startup, get the key of the first loaded page
// so the user can always go back there.
const { key } = navigation.currentEntry;
backToHomeButton.onclick = () => navigation.traverseTo(key);
// Navigate away, but the button will always work.
await navigation.navigate("/another_url").finished;
Navigating and updating state
js
navigation.navigate(url, { state: newState });
Or
js
navigation.reload({ state: newState });
Or if the state is independent from a navigation or reload:
js
navigation.updateCurrentEntry({ state: newState });
Specifications
| Specification |
|---|
| HTML # navigation-interface |