Skip to content

URL

Built-in URL and URLSearchParams classes for parsing and manipulating URLs. Available without any import.

URL

Parse a URL string into its components.

typescript
const u = new URL("https://example.com:8080/path/to/page?q=hello&page=2#section");

u.protocol  // "https:"
u.hostname  // "example.com"
u.port      // "8080"
u.host      // "example.com:8080"
u.pathname  // "/path/to/page"
u.search    // "?q=hello&page=2"
u.hash      // "#section"
u.origin    // "https://example.com:8080"
u.href      // "https://example.com:8080/path/to/page?q=hello&page=2#section"

Properties

PropertyTypeDescription
protocolstringScheme with colon — "https:"
hostnamestringHost without port — "example.com"
portstringPort number as string, or "" if absent
hoststringhostname + : + port if port present
pathnamestringPath component — "/path/to/page"
searchstringQuery string including ?, or ""
hashstringFragment including #, or ""
originstringprotocol + "//" + host
hrefstringFull URL string

URLSearchParams

Parse and manipulate query strings.

typescript
const p = new URLSearchParams("q=hello&page=2");

p.get("q")     // "hello"
p.get("page")  // "2"
p.has("q")     // true
p.has("foo")   // false

p.set("q", "world")
p.append("tag", "foo")
p.delete("page")

p.toString()   // "q=world&tag=foo"

Methods

MethodReturnsDescription
get(key)stringValue for key, or "" if absent
has(key)booleanWhether key exists
set(key, value)voidSet or replace value for key
append(key, value)voidAdd key/value (allows duplicates)
delete(key)voidRemove all entries for key
toString()stringSerialize back to query string (no leading ?)

Native Implementation

APIMaps to
URL propertiescs_url_parse_*() in c_bridges/url-bridge.c
URLSearchParams methodscs_urlsearch_*() in c_bridges/url-bridge.c