Home/Docs/Programmatic Navigation
Routing

Programmatic Navigation

The to attribute covers links, but sometimes navigation happens from code — after a form submit, a successful login, a timeout. For that, import the navigation helpers from olum:

import { push, replace, back, forward, go, pathname } from "olum";

Each helper delegates to the active router instance and works in both hash and history mode — paths are resolved against the router's root for you.

HelperActionHistory stackNative equivalent
push(path)Navigate to a new URLAdds a new entryhistory.pushState()
replace(path)Navigate to a new URLOverwrites the current entryhistory.replaceState()
back()Move backward one stepRewinds current positionhistory.back()
forward()Move forward one stepAdvances current positionhistory.forward()
go(n)Move by n steps (negative = back)Jumps to a specific indexhistory.go(n)
pathname()Read the current route pathlocation.pathname

Example

src/login/page.html
<script>
  import { replace, back } from "olum";

  const login = () => {
    // ...authenticate...
    // `replace` so the login page doesn't stay in history —
    // pressing Back afterwards won't return to the form.
    replace("/dashboard");
  };

  const cancel = () => back();
</script>

<form>
  <button type="button" onclick="login()">Sign in</button>
  <button type="button" onclick="cancel()">Cancel</button>
</form>

push vs replace

Both navigate to a new route; the difference is what the Back button does afterwards:

  • push("/cart") — adds an entry, so Back returns to the page you came from. This is what a normal link click does.
  • replace("/dashboard") — swaps the current entry, so the page you were on disappears from history. Use it for redirects (login flows, deprecated URLs) where returning to the intermediate page makes no sense.

Reading the current route

pathname() returns the current route path, normalized the same way the router matches it — leading slash, no trailing slash, and in hash mode the part after #:

import { pathname } from "olum";

if (pathname() === "/checkout") {
  // ...
}
💡

For links, prefer the to attribute — it also gives you active-link styling for free. Reach for these helpers only when navigation is triggered by logic rather than a click on a link.