|
| 1 | +--- |
| 2 | +title: NavigateEvent.canIntercept |
| 3 | +slug: Web/API/NavigateEvent/canIntercept |
| 4 | +page-type: web-api-instance-property |
| 5 | +tags: |
| 6 | + - API |
| 7 | + - canIntercept |
| 8 | + - Experimental |
| 9 | + - History |
| 10 | + - Navigate |
| 11 | + - NavigateEvent |
| 12 | + - Navigation |
| 13 | + - Navigation API |
| 14 | + - Property |
| 15 | + - Read-only |
| 16 | + - Reference |
| 17 | + - Scroll |
| 18 | + - Traversal |
| 19 | +browser-compat: api.NavigateEvent.canIntercept |
| 20 | +--- |
| 21 | + |
| 22 | +{{APIRef("Navigation API")}}{{seecompattable}} |
| 23 | + |
| 24 | +The **`canIntercept`** read-only property of the |
| 25 | +{{domxref("NavigateEvent")}} interface returns `true` if the navigation can be intercepted and have its URL rewritten, or `false` otherwise |
| 26 | + |
| 27 | +There are several rules around when a navigation can be intercepted. For example: |
| 28 | + |
| 29 | +- You can't intercept cross-origin navigations. |
| 30 | +- You can intercept `http` or `https` URLs if only the `path`, `query`, and `fragment` portions of the new URL differ from the current URL. |
| 31 | +- You can intercept `file` URLs if only the `query` and `fragment` portions of the new URL differ. |
| 32 | +- For other URL types you can intercept the navigation if only the `fragment` portion differs. |
| 33 | + |
| 34 | +See the spec for more explanation on [when a Document can have its URL rewritten](https://html.spec.whatwg.org/multipage/nav-history-apis.html#can-have-its-url-rewritten), including a table of examples. |
| 35 | + |
| 36 | +## Value |
| 37 | + |
| 38 | +A boolean value—`true` if the navigation can be intercepted, `false` if not. |
| 39 | + |
| 40 | +## Examples |
| 41 | + |
| 42 | +```js |
| 43 | +navigation.addEventListener("navigate", event => { |
| 44 | + // Some navigations, e.g. cross-origin navigations, we |
| 45 | + // cannot intercept. Let the browser handle those normally. |
| 46 | + if (!event.canIntercept) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Don't intercept fragment navigations or downloads. |
| 51 | + if (event.hashChange || event.downloadRequest !== null) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + event.intercept({ |
| 56 | + handler() { |
| 57 | + if (event.formData) { |
| 58 | + processFormDataAndUpdateUI(event.formData, event.signal); |
| 59 | + } else { |
| 60 | + doSinglePageAppNav(event.destination, event.signal); |
| 61 | + } |
| 62 | + } |
| 63 | + }); |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +## Specifications |
| 68 | + |
| 69 | +{{Specifications}} |
| 70 | + |
| 71 | +## Browser compatibility |
| 72 | + |
| 73 | +{{Compat}} |
| 74 | + |
| 75 | +## See also |
| 76 | + |
| 77 | +- [Modern client-side routing: the Navigation API](https://developer.chrome.com/docs/web-platform/navigation-api/) |
| 78 | +- [Navigation API explainer](https://github.com/WICG/navigation-api/blob/main/README.md) |
| 79 | +- Domenic Denicola's [Navigation API live demo](https://gigantic-honored-octagon.glitch.me/) |
0 commit comments