Parse URL

Break down a URL into its individual components like protocol, host, and path.

Code

Utilities
const u = new URL(url);
const parts = [
  `protocol: ${u.protocol}`,
  `host: ${u.host}`,
  u.port && `port: ${u.port}`,
  u.username && `username: ${u.username}`,
  u.password && `password: ${u.password}`,
  `pathname: ${u.pathname}`,
  u.search && `search: ${u.search}`,
  u.hash && `hash: ${u.hash}`
].filter(Boolean);
return parts.join('\n');

Parameters

URL to parse.

Browser·fetch() may be limited by CORS

More JavaScript Snippets