Skip to content

Instantly share code, notes, and snippets.

Last active Sep 7, 2022
What would you like to do?
Get the domain name from a URL in JavaScript
const url = "";
let domain = (new URL(url));
domain = domain.hostname;
console.log(domain); //
// OR strip the www
domain = domain.hostname.replace('www.','');
console.log(domain); //
// Other properties
const pathname = domain.pathname;
console.log(pathname); // blog
const protocol = domain.protocol;
console.log(protocol); // https
const search =;
console.log(search); // ?search=hello&world
Copy link

w3collective commented Dec 23, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment