web api instance method
Navigation: navigate() method
The navigate() method of the
Navigation interface navigates to a specific URL, updating any provided state in the history entries list.
Syntax
navigate(url)
navigate(url, options)
Parameters
url- : The destination URL to navigate to. Note that when calling
navigate()on another window’snavigationobject, the URL will be resolved relative to the target window’s URL, not the calling window’s URL. This matches the behavior of the History API, but not the behavior of the Location API. Note also thatjavascript:URLs are not allowed for security reasons.
- : The destination URL to navigate to. Note that when calling
optionsOptional- : An options object containing the following properties:
stateOptional- : Developer-defined information to be stored in the associated
NavigationHistoryEntryonce the navigation is complete, retrievable viagetState(). This can be any data type. You might, for example, wish to store a page visit count for analytics purposes, or store UI state details so the view can be shown exactly as the user last left it. Any data stored instatemust be structured-cloneable.
- : Developer-defined information to be stored in the associated
infoOptional- : Developer-defined information to be passed along to the
navigateevent, made available ininfo. This can be any data type. You might, for example, wish to display newly-navigated content with a different animation depending on how it was navigated to (swipe left, swipe right, or go home). A string indicating which animation to use could be passed in asinfo.
- : Developer-defined information to be passed along to the
historyOptional- : An enumerated value that sets the history behavior of this navigation. The available values are:
auto: The default value; will usually perform apushnavigation but will perform areplacenavigation under special circumstances (see theNotSupportedErrordescription below).push: Will push a newNavigationHistoryEntryonto the entries list, or fail under special circumstances (see theNotSupportedErrordescription below).replace: Will replace the currentNavigationHistoryEntry.
- : An enumerated value that sets the history behavior of this navigation. The available values are:
- : An options object containing the following properties:
Return value
An object with the following properties:
committed- : A
Promisewhich will fulfill when the visible URL has changed and a newNavigationHistoryEntryhas been created.
- : A
finished- : A
Promisewhich will fulfill when all promises returned by theintercept()handler are fulfilled. This is equivalent to thefinishedpromise fulfilling, when thenavigatesuccessevent fires.
- : A
Either one of these promises rejects if the navigation has failed for some reason.
Exceptions
DataCloneErrorDOMException- : Thrown if the
stateparameter contains values that are not structured-cloneable.
- : Thrown if the
InvalidStateErrorDOMException- : Thrown if the document is not currently active.
SyntaxErrorDOMException- : Thrown if the
urlparameter is not a valid URL.
- : Thrown if the
NotSupportedErrorDOMException- : Thrown if:
- The
historyoption is set topush, and the browser is currently showing the initialabout:blankdocument. - The
url’s scheme isjavascript.
- The
- : Thrown if:
Examples
Set up home button
function initHomeBtn() {
// Get the key of the first loaded entry
// so the user can always go back to this view.
const { key } = navigation.currentEntry;
backToHomeButton.onclick = () => {
navigation.traverseTo(key);
};
}
// Intercept navigate events, such as link clicks, and
// replace them with single-page navigations
navigation.addEventListener("navigate", (event) => {
event.intercept({
async handler() {
// Navigate to a different view,
// but the "home" button will always work.
},
});
});
A smart back button
A page-supplied “back” button can take you back, even after reload, by inspecting the previous history entries:
backButtonEl.addEventListener("click", () => {
if (
navigation.entries()[navigation.currentEntry.index - 1]?.url ===
"/product-listing"
) {
navigation.back();
} else {
// If the user arrived here in some other way
// e.g. by typing the URL directly:
navigation.navigate("/product-listing", { history: "replace" });
}
});
Using info and state
async function navigateHandler() {
await navigation.navigate(url, {
info: { animation: "swipe-right" },
state: { infoPaneOpen: true },
}).finished;
// Update application state
// …
}
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.