web api instance method
Navigation: traverseTo() method
The traverseTo() method of the Navigation interface navigates to the NavigationHistoryEntry identified by the given key.
Syntax
traverseTo(key)
traverseTo(key, options)
Parameters
key- : The
keyof theNavigationHistoryEntryto navigate to.
- : The
optionsOptional- : An options object containing the following properties:
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
- : 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
InvalidStateErrorDOMException- : Thrown if the
currentEntry’sindexvalue is -1, meaning the currentDocumentis not yet active, or if the navigation history list does not contain aNavigationHistoryEntrywith the specified key, or if the currentDocumentis unloading.
- : Thrown if the
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.
},
});
});
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.