files/en-us/web/api/navigation/index.md
{{APIRef("Navigation API")}}
The Navigation interface of the {{domxref("Navigation API", "Navigation API", "", "nocode")}} allows control over all navigation actions for the current window in one central place, including initiating navigations programmatically, examining navigation history entries, and managing navigations as they happen.
It is accessed via the {{domxref("Window.navigation")}} property.
The Navigation API only exposes history entries created in the current browsing context that have the same origin as the current page (e.g., not navigations inside embedded {{htmlelement("iframe")}}s, or cross-origin navigations), providing an accurate list of all previous history entries just for your app. This makes traversing the history a much less fragile proposition than with the older {{domxref("History API", "History API", "", "nocode")}}.
{{InheritanceDiagram}}
Inherits properties from its parent, {{DOMxRef("EventTarget")}}.
true if it is possible to navigate backwards in the navigation history
(i.e., the {{domxref("Navigation.currentEntry", "currentEntry")}} is not the first one in the history entry list),
and false if it is not.true if it is possible to navigate forwards in the navigation history
(i.e., the {{domxref("Navigation.currentEntry", "currentEntry")}} is not the last one in the history entry list),
and false if it is not.null if no navigation is currently in progress.Inherits methods from its parent, {{DOMxRef("EventTarget")}}.
Inherits events from its parent, {{DOMxRef("EventTarget")}}.
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");
}
}
// 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;
navigation.navigate(url, { state: newState });
Or
navigation.reload({ state: newState });
Or if the state is independent from a navigation or reload:
navigation.updateCurrentEntry({ state: newState });
{{Specifications}}
{{Compat}}