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.

BehaviourByFires when
In-app navigation, no reloadrouterPlain 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 pagerouterAs above, but no route matches the path
Scroll to anchor, route staysbrowser#intro, or the current path and query plus a fragment
Full page load, same tabbrowserAbsolute URL (https://…, //host/…), even to your own app · rel="external" · router disabled
New tabbrowserCtrl / Cmd + click · middle click · target="_blank"
New windowbrowserShift + click
Downloadbrowserdownload attribute · Alt / Option + click in most browsers
Opens in another frame or windowbrowserNamed target · <base target> on the page · _top / _parent when the app runs inside a frame
Mail app, dialer, script, …browsermailto:, tel:, javascript: and other schemes
Only your handler runsyouYour handler called preventDefault()
Browser default, router stays outbrowserYour 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:

LinkHandled byResult
/users/alice, A.href(route)routerIn-app navigation, new history entry
settings, ../users (relative)routerResolved like the browser does: settings on /users/alice goes to /users/settings. A <base href> is honoured
/docs?tab=2#introrouterQuery and fragment are kept in the URL; the fragment plays no part in route matching
#intro, or the current path and query plus a fragmentbrowserScrolls 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/…browserFull page load, even when the URL points at your own app
//cdn.example.com/…browserScheme-relative URLs are absolute URLs too
mailto:, tel:, blob:, data:, javascript:browserWhatever the browser does with that scheme
target="_blank", or any named targetbrowserOpens where the target says
target="_self", target=""routerSame as no target
target="_top", target="_parent"router / browserRouter when the app is not inside a frame, browser when it is
No target, but <base target="_blank"> on the pagebrowserThe base target applies, as in plain HTML
download attributebrowserDownloads the file
rel="external"browserFull 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" }
„The router used to ask a URL regex whether a link was external. The regex had never heard of 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:

ClickHandled byUsual browser result
Left click, or Enter on a focused linkrouter—
Ctrl + click (Cmd + click on macOS)browserNew tab
Shift + clickbrowserNew window
Alt + click (Option + click on macOS)browserDownload, in most browsers
Middle clickbrowserNew tab
Right clickbrowserContext 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().

„A routed link to /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."