Example 1 — Read parts
Common fields.
JavaScript
const u = new URL("https://user:[email protected]:8080/a?b=1#c");
console.log(u.protocol);
console.log(u.host);
console.log(u.hash);How It Works
- host includes port when non-default.
Use new URL(absoluteUrl) or new URL(path, base). Mutate properties to rebuild href.
const u = new URL("/docs", "https://example.com");
u.searchParams.set("page", "2");
console.log(u.href);Common fields.
const u = new URL("https://user:[email protected]:8080/a?b=1#c");
console.log(u.protocol);
console.log(u.host);
console.log(u.hash);CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful