URL

WHATWG URL Standard — parser, component accessors, and search-params live binding.

Elide implements the WHATWG URL Standard URL global backed by the Servo url crate — the reference Rust implementation of the spec. This means the parser, the host parsing (IDNA / IPv4 / IPv6), every percent-encoding code-point set, and the per-setter basic-URL-parser state overrides all match the spec by construction.

Construction

new URL(input)             // absolute URL
new URL(input, base)       // input resolved against base

Both arguments are coerced to strings. Parsing failure raises TypeError. URL(input) called without new (as a plain function) is a constructor-call error per WebIDL — use URL.parse if you want a non-throwing variant.

Static methods

MethodReturnsNotes
URL.parse(input, base?)URL | nullNon-throwing — returns null instead of raising.
URL.canParse(input, base?)booleanJust the parseability check; no object allocated.

Component accessors

Every component is both a getter and a setter except origin, which is read-only. Setter semantics follow the WHATWG spec’s basic URL parser with state overrides — invalid inputs are silently ignored (e.g. setting a non-numeric port) except for href which throws TypeError on parse failure.

ComponentGetter returnsSetter notes
hrefFull serialisation (same as toString()).Reparses the URL — throws TypeError on failure.
originASCII-serialised tuple origin (scheme://host[:port]) for tuple-origin schemes; the literal string "null" for opaque origins (file:, data:, etc.). Always a string.Read-only.
protocolScheme plus trailing :.Trailing : optional; silently ignored on forbidden transitions.
usernameRaw username component.Percent-encoded via the userinfo set.
passwordRaw password component.Empty string clears the password.
hosthostname + : + port if a port is present.Accepts either "host" or "host:port".
hostnameHost portion (bracketed for IPv6).Empty string clears the host.
portStringified port or "" for the default.Non-numeric input silently ignored; "" clears.
pathnamePath (always starts with / for authority URLs).Normalised per the spec (dot segments resolved).
search"?" + query or "".Leading ? optional; empty string clears the query.
searchParamsURLSearchParams — see below.Read-only. Live-bound (see below).
hash"#" + fragment or "".Leading # optional; empty string clears the fragment.

searchParams live binding

URL.searchParams returns the same URLSearchParams object on every access for a given URL instance (WebIDL [SameObject]), and the two stay in sync in both directions:

const u = new URL("https://example.com/?a=1");
u.searchParams.append("b", "2");
u.href;                 // "https://example.com/?a=1&b=2"
u.search = "?c=3";      // replaces the query
u.searchParams.size;    // 1 — the previously-cached view reflects the new state

Every mutation on the returned URLSearchParams is re-serialised and fed into URL.search; reassigning URL.href or URL.search refreshes the cached view’s pair list from the URL’s current query.

Origin serialisation

URL.origin serialises per WHATWG spec:

  • http / https / ws / wss / ftp: "scheme://host[:port]".
  • file:: "null" (opaque origin).
  • Other non-special schemes: "null" (opaque origin).

Examples

const u = new URL("https://user:pw@example.com:8080/a/b?x=1&y=2#frag");
u.protocol;          // "https:"
u.host;              // "example.com:8080"
u.origin;            // "https://example.com:8080"
u.pathname;          // "/a/b"

// IDN host — punycoded on the host serialisation side.
new URL("https://日本.jp/").hostname;   // "xn--wgv71a.jp"

// IPv6 literal, brackets preserved.
new URL("http://[::1]:9000/").host;    // "[::1]:9000"

// Non-throwing parse.
URL.canParse("::::bad");               // false
URL.parse("https://x/").href;          // "https://x/"

Compatibility notes

  • URL is installed as a global on every JavaScript realm in Elide.
  • Implementation is backed by the url Rust crate, so behaviour matches Firefox / Servo and is broadly compatible with Node.js, Deno, and Chromium.
  • Native allocations backing each URL handle are released by a JVM Cleaner when the JS object becomes unreachable; no explicit .dispose() is required.

See also