JS: Parse URL

By Xah Lee. Date: .

To parse url into parts, use:

new URL(url)

const xx = new URL("http://www.example.com:49158/a/b/c?x=1&y=2#xyz");

console.log(xx);

/*

URL {
  href: "http://www.example.com:49158/a/b/c?x=1&y=2#xyz",
  origin: "http://www.example.com:49158",
  protocol: "http:",
  username: "",
  password: "",
  host: "www.example.com:49158",
  hostname: "www.example.com",
  port: "49158",
  pathname: "/a/b/c",
  hash: "#xyz",
  search: "?x=1&y=2"
}

*/

// note, port is omitted when it is same as default

JavaScript DOM, Get URL, Set URL