Link Clicks
Which clicks the router turns into in-app navigation, and which it leaves to the browser.
a { href(Nav.userProfile("alice")); +"Alice" } // in-app
a(href = "/static/manual.pdf", target = "_blank") { +"Manual" } // new tab
a(href = "https://example.com") { +"Elsewhere" } // full page load
With usePathStrategy() the router listens for clicks on the whole page. It takes over a click only when
it can serve it as an in-app navigation; everything else goes to the browser, which does what it would do on any
other website. A link built with A.href(route) needs no click handler.
At a glance
What a click on a link does, and when. Modifier keys and middle click come first: they reach the browser whatever the link says.
| Behaviour | By | Fires when |
|---|---|---|
| In-app navigation, no reload | router | Plain left click or Enter, on a relative link (/users, settings, A.href(route)) that resolves to your origin — with no target other than _self, no download, no rel="external", and no handler that called preventDefault() |
In-app, catchAll page | router | As above, but no route matches the path |
| Scroll to anchor, route stays | browser | #intro, or the current path and query plus a fragment |
| Full page load, same tab | browser | Absolute URL (https://…, //host/…), even to your own app · rel="external" · router disabled |
| New tab | browser | Ctrl / Cmd + click · middle click · target="_blank" |
| New window | browser | Shift + click |
| Download | browser | download attribute · Alt / Option + click in most browsers |
| Opens in another frame or window | browser | Named target · <base target> on the page · _top / _parent when the app runs inside a frame |
| Mail app, dialer, script, … | browser | mailto:, tel:, javascript: and other schemes |
| Only your handler runs | you | Your handler called preventDefault() |
| Browser default, router stays out | browser | Your handler called stopPropagation() but not preventDefault() |
Which links
For a plain left click, the router handles the link only if every row below says router:
| Link | Handled by | Result |
|---|---|---|
/users/alice, A.href(route) | router | In-app navigation, new history entry |
settings, ../users (relative) | router | Resolved like the browser does: settings on /users/alice goes to /users/settings. A <base href> is honoured |
/docs?tab=2#intro | router | Query and fragment are kept in the URL; the fragment plays no part in route matching |
#intro, or the current path and query plus a fragment | browser | Scrolls to the anchor. The active route stays, and nothing re-renders. Under a <base href> that points elsewhere, #intro is a link to that page, as in plain HTML |
https://…, http://localhost:8080/… | browser | Full page load, even when the URL points at your own app |
//cdn.example.com/… | browser | Scheme-relative URLs are absolute URLs too |
mailto:, tel:, blob:, data:, javascript: | browser | Whatever the browser does with that scheme |
target="_blank", or any named target | browser | Opens where the target says |
target="_self", target="" | router | Same as no target |
target="_top", target="_parent" | router / browser | Router when the app is not inside a frame, browser when it is |
No target, but <base target="_blank"> on the page | browser | The base target applies, as in plain HTML |
download attribute | browser | Downloads the file |
rel="external" | browser | Full page load. The escape hatch for same-origin URLs served by your backend |
A same-origin path the SPA has no route for is still routed in-app, and lands on catchAll — or on
nothing but a console error, without one. If
/api/export.csv or /oauth/start is served by the backend, say so on the link:
a(href = "/api/export.csv") { attributes["download"] = ""; +"Export" }
a(href = "/oauth/github/start") { attributes["rel"] = "external"; +"Sign in with GitHub" } localhost, so on a dev machine an absolute link to the app itself was treated as a path, and you ended
up at http://localhost:8080/http://localhost:8080/page. It checks what a link looks like now, not what
a regex thinks of it."
Which clicks
For a link the router would handle, the way it is clicked decides:
| Click | Handled by | Usual browser result |
|---|---|---|
| Left click, or Enter on a focused link | router | — |
| Ctrl + click (Cmd + click on macOS) | browser | New tab |
| Shift + click | browser | New window |
| Alt + click (Option + click on macOS) | browser | Download, in most browsers |
| Middle click | browser | New tab |
| Right click | browser | Context menu, "Open in new tab" etc. |
Your own click handlers
If a handler on the link, or on anything inside it, calls preventDefault(), the router leaves the click
alone. Kraft's onClick does not do that by itself:
// Fine: the router handles the plain click, the browser handles the rest
a { href(Nav.about()); +"About" }
// Handled twice: onClick navigates, then the router sees an unhandled click
a {
href(Nav.about())
onClick { evt -> router.navToUri(evt, Nav.about()) }
+"About"
}
// Your handler only, the browser and the router stay out
a {
href(Nav.about())
onClick { evt -> evt.preventDefault(); openAboutDialog() }
+"About"
}
To keep the browser's default but stop the router, call evt.stopPropagation() without
preventDefault(): the click never reaches the router's listener on window.
router.navToUri(evt, route) is for elements that are not links, such as buttons. It opens a new tab on
Ctrl / Cmd + click and navigates in-app otherwise. Browsers do not fire click for the middle button, so
wire it with onClickOrAuxClick if middle click should open a tab too.
Hash strategy
With useHashStrategy() the router takes plain primary clicks on route links only:
A.href(route) renders #/about, and a click on a link whose href starts with
#/ goes through router.navToUri, so a leave guard can hold it before the browser writes the
address. Modifier keys, other buttons, target and download are honoured as above; any other
link stays plain browser behaviour. A link whose own handler already navigated is left alone, so it does not
navigate twice.
An in-page anchor such as #intro does not work in hash mode: the hash is the route, so it
navigates to the route intro. Scroll with element.scrollIntoView() instead.
When the router is disabled
router.disable() hands every click to the browser until router.enable().
/docs#intro from another page lands at the top of /docs, not at
#intro. The router scrolls to the top on every page change and does not look for the anchor
afterwards. On the same page, #intro is the browser's job and works."