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 a another window's navigation object, 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.
options Optional
-
An options object containing the following properties:
-
state
-
Developer-defined information to be stored in the associated NavigationHistoryEntry once the navigation is complete, retrievable via getState(). 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 in state must be structured-cloneable.
-
info
-
Developer-defined information to be passed along to the navigate event, made available in NavigateEvent.info. 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 as info.
-
history
-
An enumerated value that sets the history behavior of this navigation. The available values are:
auto: The default value; will usually perform a push navigation but will perform a replace navigation under special circumstances (see the NotSupportedError description below).
push: Will push a new NavigationHistoryEntry onto the entries list, or fail under special circumstances (see the NotSupportedError description below).
replace: Will replace the current NavigationHistoryEntry.
Return value
An object with the following properties:
-
committed
-
A Promise which will fulfill when the visible URL has changed and a new NavigationHistoryEntry has been created.
-
finished
-
A Promise which will fulfill when all promises returned by the intercept() handler are fulfilled. This is equivalent to the NavigationTransition.finished promise fulfilling, when the navigatesuccess event fires.
Either one of these promises rejects if the navigation has failed for some reason.
Exceptions
DataCloneError DOMException
-
Thrown if the state parameter had values included in it that are not structured-cloneable.
SyntaxError DOMException
-
Thrown if the url parameter is not a valid URL.
NotSupportedError DOMException
-
Thrown if the history option is set to push, and any of the following special circumstances are true:
- The browser is currently showing the initial
about:blank document.
- The
url's scheme is javascript.
Examples
function initHomeBtn() {
const { key } = navigation.currentEntry;
backToHomeButton.onclick = () => {
navigation.traverseTo(key);
};
}
navigation.addEventListener("navigate", (event) => {
event.intercept({
async handler() {
},
});
});
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 {
navigation.navigate("/product-listing", { history: "replace" });
}
});
Using info and state
async function navigateHandler() {
await navigation.navigate(url, {
info: { animation: "swipe-right" },
state: { infoPaneOpen: true },
});
}
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
navigate |
102 |
102 |
No |
No |
88 |
No |
102 |
102 |
No |
70 |
No |
19.0 |
See also