Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add docs for the Navigation API #21960

Merged
merged 56 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
f74c58c
add docs for the Navigation API
chrisdavidmills Oct 31, 2022
91e37e2
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 1, 2022
388f304
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 1, 2022
a9ca266
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 1, 2022
0c7dd0d
Merge branch 'main' into add-navigation-api-ref-docs
chrisdavidmills Nov 1, 2022
03a56c7
updating landing page according to domenics comments
chrisdavidmills Nov 1, 2022
6b7a904
remaining interface pages plus assorted fixes
chrisdavidmills Nov 2, 2022
9a2b3ba
add all member pages for Navigate
chrisdavidmills Nov 4, 2022
fad99a3
making fixes for domenic review comments
chrisdavidmills Nov 4, 2022
fc0e240
fix macro error
chrisdavidmills Nov 4, 2022
c8aeb47
Merge branch 'main' into add-navigation-api-ref-docs
Nov 4, 2022
c0ed753
add member pages for NavigationDestination and NavigationHistoryEntry
chrisdavidmills Nov 4, 2022
44c0b18
Merge branch 'add-navigation-api-ref-docs' of github.com:chrisdavidmi…
chrisdavidmills Nov 4, 2022
72f8ce2
attempt to fix folder name casing
chrisdavidmills Nov 4, 2022
b2e1e36
fix casing issue in directory name
chrisdavidmills Nov 7, 2022
4554e97
add all remaining pages, fix flaws
chrisdavidmills Nov 7, 2022
d264c4f
Merge branch 'main' into add-navigation-api-ref-docs
chrisdavidmills Nov 7, 2022
1e96c03
adding description of when disposal occurs
chrisdavidmills Nov 8, 2022
0e18d96
Merge branch 'add-navigation-api-ref-docs' of github.com:chrisdavidmi…
chrisdavidmills Nov 8, 2022
de7f600
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 9, 2022
43f9c1f
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 9, 2022
f3e94db
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 9, 2022
5a47f9e
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 9, 2022
b4a42f3
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 9, 2022
40f93b0
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 9, 2022
dc435da
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 9, 2022
12a51d2
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 9, 2022
33a7e8e
Update files/en-us/web/api/navigation_api/index.md
chrisdavidmills Nov 9, 2022
5938b37
adding fixes for wbamberg comments
chrisdavidmills Nov 9, 2022
ecee4c7
Merge branch 'add-navigation-api-ref-docs' of github.com:chrisdavidmi…
chrisdavidmills Nov 9, 2022
6333b97
adding fixes for wbamberg comments
chrisdavidmills Nov 9, 2022
8725f8e
making fixes for wbamberg comments
chrisdavidmills Nov 10, 2022
03efe1e
Update files/en-us/web/api/navigation/index.md
chrisdavidmills Nov 10, 2022
fa2f482
Update files/en-us/web/api/navigation/updatecurrententry/index.md
chrisdavidmills Nov 10, 2022
3bb1bed
Update files/en-us/web/api/navigation/updatecurrententry/index.md
chrisdavidmills Nov 10, 2022
251848f
Update files/en-us/web/api/navigation/updatecurrententry/index.md
chrisdavidmills Nov 10, 2022
b16a267
Update files/en-us/web/api/navigation/updatecurrententry/index.md
chrisdavidmills Nov 10, 2022
127c770
Update files/en-us/web/api/navigation/traverseto/index.md
chrisdavidmills Nov 10, 2022
3761a1d
Update files/en-us/web/api/navigation/back/index.md
chrisdavidmills Nov 10, 2022
991c81a
Update files/en-us/web/api/navigation/cangoback/index.md
chrisdavidmills Nov 10, 2022
e816cb1
Update files/en-us/web/api/navigation/cangoforward/index.md
chrisdavidmills Nov 10, 2022
9696dbc
Update files/en-us/web/api/navigation/currententry/index.md
chrisdavidmills Nov 10, 2022
504f04f
Update files/en-us/web/api/navigation/forward/index.md
chrisdavidmills Nov 10, 2022
ed352c5
Update files/en-us/web/api/navigation/navigate/index.md
chrisdavidmills Nov 10, 2022
b0a10a5
Update files/en-us/web/api/navigation/navigate/index.md
chrisdavidmills Nov 10, 2022
5e6be2a
Update files/en-us/web/api/navigation/navigate/index.md
chrisdavidmills Nov 10, 2022
4ad513f
Update files/en-us/web/api/navigation/reload/index.md
chrisdavidmills Nov 10, 2022
af10131
Update files/en-us/web/api/navigation/reload/index.md
chrisdavidmills Nov 10, 2022
3b887e3
Update files/en-us/web/api/navigation/transition/index.md
chrisdavidmills Nov 10, 2022
9c9e8da
Update files/en-us/web/api/navigation/traverseto/index.md
chrisdavidmills Nov 10, 2022
d1b7e04
Update files/en-us/web/api/navigation/traverseto/index.md
chrisdavidmills Nov 10, 2022
6448d5b
fixing linter trailing space errors
chrisdavidmills Nov 10, 2022
f4b5752
add structured-clonable information to navigate()
chrisdavidmills Nov 14, 2022
7f81b8b
add structured-clonable information to reload() and updateCurrentEntry()
chrisdavidmills Nov 15, 2022
1476651
making fixes in response to wbamberg navigationhistoryentry comments
chrisdavidmills Nov 16, 2022
1805606
moooooar fixes for wbamberg comments
chrisdavidmills Nov 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions files/en-us/web/api/navigateevent/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
title: NavigateEvent
slug: Web/API/NavigateEvent
page-type: web-api-interface
tags:
- API
- History
- Interface
- Landing
- Navigate
- NavigateEvent
- Navigation API
- Scroll
- Traversal
browser-compat: api.NavigateEvent
---

{{DefaultAPISidebar("Navigation API")}}

The **`NavigateEvent`** interface of the {{domxref("Navigation API")}} is the event object for the {{domxref("Navigation/navigate_event", "navigate")}} event, which fires when [any type of navigation](https://github.com/WICG/navigation-api#appendix-types-of-navigations) is initiated (this includes usage of History API features like {{domxref("History.go()")}}). `NavigateEvent` provides access to information about that navigation, and allows developers to intercept and control the navigation handling.
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved

{{InheritanceDiagram}}

## Constructor

- {{domxref("NavigateEvent.NavigateEvent", "NavigateEvent()")}}
- : Creates a new `NavigateEvent` object instance.

## Instance properties

_Inherits properties from its parent, {{DOMxRef("Event")}}._

- {{domxref("NavigateEvent.canIntercept", "canIntercept")}} {{ReadOnlyInline}}
- : Returns `true` if the navigation can be intercepted, or `false` otherwise (e.g. you can't intercept a cross-origin navigation).
- {{domxref("NavigateEvent.destination", "destination")}} {{ReadOnlyInline}}
- : Returns the URL being navigated to.
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved
- {{domxref("NavigateEvent.downloadRequest", "downloadRequest")}} {{ReadOnlyInline}}
- : Returns the filename of the file requested for download, in the case of a download navigation (e.g. an {{htmlelement("a")}} or {{htmlelement("area")}} element with a `download` attribute), or `null` otherwise.
- {{domxref("NavigateEvent.formData", "formData")}} {{ReadOnlyInline}}
- : Returns the {{domxref("FormData")}} object representing the submitted data in the case of a `POST` form submission, or null otherwise.
- {{domxref("NavigateEvent.hashChange", "hashChange")}} {{ReadOnlyInline}}
- : Returns `true` if the navigation is a fragment navigation (i.e. to a fragment identifier in the same document), or `false` otherwise.
- {{domxref("NavigateEvent.info", "info")}} {{ReadOnlyInline}}
- : Returns the `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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null?

An arbitrary JavaScript value passed via navigation APIs that initiated this navigation, or null if the navigation was initiated by the user or via a non-navigation API.

(https://wicg.github.io/navigation-api/#dom-navigateevent-info)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment.

- {{domxref("NavigateEvent.navigationType", "navigationType")}} {{ReadOnlyInline}}
- : Returns the type of the navigation, which is one of `push`, `reload`, `replace`, or `traverse`.
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved
- {{domxref("NavigateEvent.signal", "signal")}} {{ReadOnlyInline}}
- : Returns an {{domxref("AbortSignal")}}, which will become aborted if the navigation is cancelled (e.g. by the user pressing the browser's "Stop" button).
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved
- {{domxref("NavigateEvent.userInitiated", "userInitiated")}} {{ReadOnlyInline}}
- : Returns `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.

## Instance methods

_Inherits methods from its parent, {{DOMxRef("Event")}}._

- {{domxref("NavigateEvent.intercept", "intercept()")}}
- : Intercepts this navigation, turning it into a same-document navigation to the {{domxref("NavigateEvent.destination", "destination")}} URL. It can accept a handler that defines what the navigation handling behavior should be, plus options to control autofocus ansd scroll behavior as desired.
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved
- {{domxref("NavigateEvent.scroll", "scroll()")}}
- : Can be called to manually trigger the browser-driven scrolling behavior that occurs in response to the navigation, if you want it to happen before the navigation handling has completed.

## Examples

### Handling a navigation using `intercept()`

```js
navigation.addEventListener('navigate', navigateEvent => {
// Exit early if this navigation shouldn't be intercepted,
// e.g. if the navigation is cross-origin, or a download request
if (shouldNotIntercept(navigateEvent)) return;

const url = new URL(navigateEvent.destination.url);

if (url.pathname.startsWith('/articles/')) {
navigateEvent.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:** Previous to the Navigation API being 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> **Note:** Previous to the Navigation API being 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.
> **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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, clumsy writing. Updated!


### Handling scrolling using `scroll()`

```js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be great to have a sentence or two explaining what this code sample is doing and why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call; I've added a paragraph to explain it.

navigation.addEventListener('navigate', navigateEvent => {
if (shouldNotIntercept(navigateEvent)) return;
const url = new URL(navigateEvent.destination.url);

if (url.pathname.startsWith('/articles/')) {
navigateEvent.intercept({
async handler() {
const articleContent = await getArticleContent(url.pathname);
renderArticlePage(articleContent);
navigateEvent.scroll();

const secondaryContent = await getSecondaryContent(url.pathname);
addSecondaryContent(secondaryContent);
},
});
}
});
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- [Modern client-side routing: the Navigation API](https://developer.chrome.com/docs/web-platform/navigation-api/)
- [Navigation API explainer](https://github.com/WICG/navigation-api/blob/main/README.md)
- Domenic Denicola's [Navigation API live demo](https://gigantic-honored-octagon.glitch.me/)
141 changes: 141 additions & 0 deletions files/en-us/web/api/navigation/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
title: Navigation
slug: Web/API/Navigation
page-type: web-api-interface
tags:
- API
- History
- Interface
- Landing
- Navigate
- Navigation
- Navigation API
- Scroll
- Traversal
browser-compat: api.Navigation
---

{{DefaultAPISidebar("Navigation API")}}

The **`Navigation`** interface of the {{domxref("Navigation API")}} allows control over all navigation actions for the current `window` in one central place, including initiating navigations programmatically, introspecting navigation history entries, and managing navigations as they happen.
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved

It is accessed via the {{domxref("Window.navigation")}} property.

The Navigation API only creates history entries created directly in the application document (i.e. not {{htmlelement("iframe")}} navigations or cross-origin navigations), providing an accurate list of all previous history entries just for your app. This makes traversing the history a much less fragile proposition than the older {{domxref("History API")}}.
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved

{{InheritanceDiagram}}

## Instance properties

_Inherits properties from its parent, {{DOMxRef("EventTarget")}}._

- {{domxref("Navigation.canGoBack", "canGoBack")}} {{ReadOnlyInline}}
- : Returns `true` if it is possible to navigate backwards in the navigation history
(i.e. the {{domxref("Navigation.currentEntry", "currentEntry")}} is not the first one in the history entry list),
and `false` if it is not.
- {{domxref("Navigation.canGoForward", "canGoForward")}} {{ReadOnlyInline}}
- : Returns `true` if it is possible to navigate forwards in the navigation history
(i.e. the {{domxref("Navigation.currentEntry", "currentEntry")}} is not the last one in the history entry list),
and `false` if it is not.
- {{domxref("Navigation.currentEntry", "currentEntry")}} {{ReadOnlyInline}}
- : Returns a {{domxref("NavigationHistoryEntry")}} object representing the location the user is currently
navigated to right now.
- {{domxref("Navigation.transition", "transition")}} {{ReadOnlyInline}}
- : Returns a {{domxref("NavigationTransition")}} object representing the status of an in-progress navigation,
which can be used to track it. Returns `null` in no navigation is currently in progress.
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved

### `Navigation` events
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved

- {{domxref("Navigation/currententrychange_event", "currententrychange")}}
- : Fired when the {{domxref("Navigation.currentEntry")}} has changed.
- {{domxref("Navigation/navigate_event", "navigate")}}
- : Fired when [any type of navigation](https://github.com/WICG/navigation-api#appendix-types-of-navigations) is initiated, allowing you to intercept as required.
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved
- {{domxref("Navigation/navigateerror_event", "navigateerror")}}
- : Fired when a navigation fails.
- {{domxref("Navigation/navigatesuccess_event", "navigatesuccess")}}
- : Fired when a successful navigation has completed.
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved

## Instance methods

_Inherits methods from its parent, {{DOMxRef("EventTarget")}}._

- {{domxref("Navigation.back", "back()")}}
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved
- : Navigates backwards by one entry in the navigation history.
- {{domxref("Navigation.entries", "entries()")}}
- : Returns an array of {{domxref("NavigationHistoryEntry")}} objects representing all existing history entries.
- {{domxref("Navigation.forward", "forward()")}}
- : Navigates forwards by one entry in the navigation history.
- {{domxref("Navigation.navigate", "navigate()")}}
- : Navigates to a specific URL, updating any provided `state` in the history entries list.
- {{domxref("Navigation.reload", "reload()")}}
- : Reloads the current URL, updating any provided `state` in the history entries list.
- {{domxref("Navigation.traverseTo", "traverseTo()")}}
- : Navigates to a specific {{domxref("NavigationHistoryEntry")}} by {{domxref("NavigationHistoryEntry.key", "key")}},
passing any provided `info` to the corresponding {{domxref("NavigateEvent")}}.
- {{domxref("Navigation.updateCurrentEntry", "updateCurrentEntry()")}}
- : Updates the `state` of the {{domxref("Navigation.currentEntry","currentEntry")}},
in cases where the state change will be independent from a navigation or reload.

## Examples

### Moving forwards and backwards in the history

```js
if(navigation.canGoBack) {
navigation.back();
} else {
displayBanner('You are on the first page');
}

...
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved

if(navigation.canGoForward) {
navigation.forward();
} else {
displayBanner('You are on the last page');
}
```

### Traversing to a specific history entry

```js
// On JS startup, get the key of the first loaded page
// so the user can always go back there.
const {key} = navigation.currentEntry;
backToHomeButton.onclick = () => navigation.traverseTo(key);

// Navigate away, but the button will always work.
await navigation.navigate('/another_url').finished;
chrisdavidmills marked this conversation as resolved.
Show resolved Hide resolved
```

### Navigating and updating state

```js
navigation.navigate(url, {state: newState});
```

Or

```js
navigation.reload({state: newState});
```

Or if the state is independent from a navigation or reload:

```js
navigation.updateCurrentEntry({state: newState});
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- [Modern client-side routing: the Navigation API](https://developer.chrome.com/docs/web-platform/navigation-api/)
- [Navigation API explainer](https://github.com/WICG/navigation-api/blob/main/README.md)
- Domenic Denicola's [Navigation API live demo](https://gigantic-honored-octagon.glitch.me/)
Loading