files/en-us/web/api/navigation/navigate/index.md
{{APIRef("Navigation API")}}
The navigate() method of the
{{domxref("Navigation")}} interface navigates to a specific URL, updating any provided state in the history entries list.
navigate(url)
navigate(url, options)
url
navigate() on 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. Note also that javascript: URLs are not allowed for security reasons.options {{optional_inline}}
state {{optional_inline}}
state must be structured-cloneable.info {{optional_inline}}
info.history {{optional_inline}}
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 {{domxref("NavigationHistoryEntry")}} onto the entries list, or fail under special circumstances (see the NotSupportedError description below).replace: Will replace the current {{domxref("NavigationHistoryEntry")}}.An object with the following properties:
committed
finished
intercept() handler are fulfilled. This is equivalent to the {{domxref("NavigationTransition.finished")}} promise fulfilling, when the {{domxref("Navigation/navigatesuccess_event", "navigatesuccess")}} event fires.Either one of these promises rejects if the navigation has failed for some reason.
DataCloneError {{domxref("DOMException")}}
state parameter contains values that are not structured-cloneable.InvalidStateError {{domxref("DOMException")}}
SyntaxError {{domxref("DOMException")}}
url parameter is not a valid URL.NotSupportedError {{domxref("DOMException")}}
history option is set to push, and the browser is currently showing the initial about:blank document.url's scheme is javascript.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 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" });
}
});
async function navigateHandler() {
await navigation.navigate(url, {
info: { animation: "swipe-right" },
state: { infoPaneOpen: true },
}).finished;
// Update application state
// …
}
{{Specifications}}
{{Compat}}