files/en-us/web/api/navigateevent/index.md
{{APIRef("Navigation API")}}
The NavigateEvent interface of the {{domxref("Navigation API", "Navigation API", "", "nocode")}} is the event object for the {{domxref("Navigation/navigate_event", "navigate")}} event, which fires when any type of navigation is initiated (this includes usage of {{domxref("History API", "History API", "", "nocode")}} features like {{domxref("History.go()")}}). NavigateEvent provides access to information about that navigation, and allows developers to intercept and control the navigation handling.
{{InheritanceDiagram}}
NavigateEvent object instance.Inherits properties from its parent, {{DOMxRef("Event")}}.
true if the navigation can be intercepted, or false otherwise (e.g., you can't intercept a cross-origin navigation).download attribute), or null otherwise.POST form submission, or null otherwise.true if the navigation is a fragment navigation (i.e., to a fragment identifier in the same document), or false otherwise.true if the user agent performed a visual transition for this navigation before dispatching this event, or false otherwise.info data value passed by the initiating navigation operation (e.g., {{domxref("Navigation.back()")}}, or {{domxref("Navigation.navigate()")}}), or undefined if no info data was passed.push, reload, replace, or traverse.true if the navigation was initiated by the user (e.g., by clicking a link, submitting a form, or pressing the browser's "Back"/"Forward" buttons), or false otherwise.Inherits methods from its parent, {{DOMxRef("Event")}}.
focusReset and scroll options to enable or disable the browser's default focus and scrolling behavior as desired.intercept()navigation.addEventListener("navigate", (event) => {
// Exit early if this navigation shouldn't be intercepted,
// e.g. if the navigation is cross-origin, or a download request
if (shouldNotIntercept(event)) return;
const url = new URL(event.destination.url);
if (url.pathname.startsWith("/articles/")) {
event.intercept({
async handler() {
// The URL has already changed, so show a placeholder while
// fetching the new content, such as a spinner or loading page
renderArticlePagePlaceholder();
// Fetch the new content and display when ready
const articleContent = await getArticleContent(url.pathname);
renderArticlePage(articleContent);
},
});
}
});
[!NOTE] Before the Navigation API was available, to do something similar you'd have to listen for all click events on links, run
e.preventDefault(), perform the appropriate {{domxref("History.pushState()")}} call, then set up the page view based on the new URL. And this wouldn't handle all navigations — only user-initiated link clicks.
scroll()In this example of intercepting a navigation, the handler() function starts by fetching and rendering some article content, but then fetches and renders some secondary content afterwards. It makes sense to scroll the page to the main article content as soon as it is available so the user can interact with it, rather than waiting until the secondary content is also rendered. To achieve this, we have added a {{domxref("NavigateEvent.scroll", "scroll()")}} call between the two.
navigation.addEventListener("navigate", (event) => {
if (shouldNotIntercept(event)) return;
const url = new URL(event.destination.url);
if (url.pathname.startsWith("/articles/")) {
event.intercept({
async handler() {
const articleContent = await getArticleContent(url.pathname);
renderArticlePage(articleContent);
event.scroll();
const secondaryContent = await getSecondaryContent(url.pathname);
addSecondaryContent(secondaryContent);
},
});
}
});
{{Specifications}}
{{Compat}}