Vanili js api url hay lắm đó về Link cách bóc tách các thành phần trong url - PhucVM2019/frontEnd GitHub Wiki

https://jsbin.com/loganiwozo/5/edit?js,console&fbclid=IwAR0sQR4P0FPOwXWbSBSeXaqCFKLtPXt5obvCHpDrNCk1eHyJdKtpxlv5ZVs

console.clear();


const url = new URL(
  "https://superheroes.com/about?num=1&greeting=hello#contact"
);
console.log(url.hash); // => #contact
console.log(url.hostname); // => superheroes.com
console.log(url.pathname); // => /about
console.log(url.protocol); // => https:
console.log(url.search); // => ?num=1&greeting=hello
console.log(url.href); // => https://superheroes.com/about?num=1&greeting=hello#contact

// you can also set a different hash easily
url.hash = "photo";
console.log('----After changing the hash----')
console.log(url.href); // => https://superheroes.com/about?num=1&greeting=hello#photo