Reference · Browser

navigate

signature
navigate(str) -> null

What it does

Goes to another address from a component's code, as a click on a link would. On a page where a component has called url(), the address changes without a reload: the history gets an entry, the page scrolls to the top, and what read url() is worked out again. On a page where none has, the browser loads the page. "#spot" goes to an element of the same page, and "/page#spot" to the spot once the new page shows it. A javascript: or vbscript: URL is an error, in the browser and on the server (since 0.7). It gives null; on the server it does nothing, since there is no page to move.

Examples

in a program
server fn create_order(items: list) -> int = db.insert("orders", { items: json.stringify(items) })component Checkout(items: list) {    state busy = false    <button disabled={busy} @click={() => {        busy = true        let id = create_order(items)        if is_err(id) { busy = false } else { navigate(`/orders/${id}`) }    }}>Place order</button>}
in a program
component Search() {    state q = ""    <form @submit={() => navigate(`/results?q=${url_encode(q)}`)}>        <input value={q}/>    </form>}

Edge cases

print(navigate("/orders/7"))print(navigate("#top"), type_of(navigate("/")))
output
nullnull null

Errors

A path that is not a string, and a javascript: or vbscript: URL, are runtime errors, not error values, on the server as in the browser; the refusal names the scheme it was given.

this one fails
print(navigate(42))
output
line 1: `navigate` expects a str as argument 1, got int
this one fails
print(navigate("javascript:alert(1)"))
output
line 1: `navigate` is not given a javascript: URL

Chaining

navigate ends a handler; the address is often built with a template and url_encode:

in a program
@click={() => navigate(`/search?q=${url_encode(q)}&page=1`)}

See also

url, redirect, url_encode, ask

Thread
6Useful? Like it, or add to its thread below.

Thread 0 posts

On navigate(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo