const url = new URL("https://user:pw@example.com:8080/a?x=1#frag");
url.host; // "example.com:8080"
url.pathname; // "/a"
url.origin; // "https://example.com:8080"new URL(input, base?) lève une TypeError en cas d’échec de l’analyse.
URL.parse(input, base?) renvoie plutôt null, et
URL.canParse(input, base?) renvoie un booléen.
Composants
Les composants modifiables sont href, protocol, username, password,
host, hostname, port, pathname, search et hash. origin et
searchParams sont des attributs en lecture seule.
Les noms d’hôte sont normalisés selon IDNA et les littéraux IPv6 conservent leurs crochets.
new URL("https://日本.jp/").hostname; // "xn--wgv71a.jp"
new URL("http://[::1]:9000/").host; // "[::1]:9000"La plupart des affectations de composants invalides sont ignorées. Affecter un
href invalide lève une TypeError. Les ports par défaut se sérialisent en un
port vide.
Paramètres de recherche dynamiques
const url = new URL("https://example.com/?a=1");
const params = url.searchParams;
params.append("b", "2");
url.href; // "https://example.com/?a=1&b=2"
url.search = "?c=3";
params.toString(); // "c=3"Le même objet URLSearchParams est renvoyé à chaque accès.
Voir aussi
Reference
Status: Full · 8,682 of 9,046 suite tests pass (96.0%) · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
URL | class | Full | URL.parse(), URL.canParse(), href, origin, protocol, username, password, host, hostname, port, pathname, search, searchParams, hash, toJSON() |
URLSearchParams | class | Full | size, append(), delete(), get(), getAll(), has(), set(), sort(), iteration |
Tests: wpt-wintertc url: 8,682 pass, 364 fail, 0 error, 2,251 skipped. Failing: url/toascii.window.js (298), url/IdnaTestV2.any.js (30), url/url-constructor.any.js (12), url/url-setters.any.js (12), url/urlsearchparams-constructor.any.js (6), url/urlencoded-parser.any.js (2), url/idlharness.any.js, url/url-origin.any.js, and 2 more.