Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

URL — parsing and modifying addresses

URL separates an address into components, resolves relative references, and lets you modify query parameters.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

new URL(url, base?)

URL is a WHATWG API, not ECMAScript. new URL(url, base?) parses an address; relative references require a base. Parsing failure throws TypeError.

href exposes the complete serialized address, while pathname exposes its path. For HTTP(S), origin contains the scheme, host, and any port, without the path. Base resolution is not concatenation: a trailing / matters.

searchParams is a linked URLSearchParams object: modifying it updates the address. Pass values without pre-encoding; spaces serialize as +. Successful parsing does not establish safety — check allowed protocol and origin values.

Examples

const url = new URL("../items", "https://example.com/app/docs/");
url.searchParams.set("q", "red shoes");
console.log(url.pathname);
console.log(url.href);

Result

/app/items
https://example.com/app/items?q=red+shoes

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources