var url = window.location.href;
var domain = url.replace('http://','').replace('https://','').split(/[/?#]/)[0];
window.location.hostname
var url = window.location.href;
var domain = url.replace('http://','').replace('https://','').split(/[/?#]/)[0];
> Site:
https://www.google.com/search?q=js+get+domain
> window.location.hostname
'www.google.com'
> window.location.hostname.replace("www.","")
'google.com'
window.location.hostname
const url = 'http://www.youtube.com/watch?v=ClkQA2Lb_iE';
const { hostname } = new URL(url);
console.assert(hostname === 'www.youtube.com'); // true
> Site:
https://www.google.com/search?q=js+get+domain
> window.location.hostname
'www.google.com'
> window.location.hostname.replace("www.","")
'google.com'
const url = 'http://www.youtube.com/watch?v=ClkQA2Lb_iE';
const { hostname } = new URL(url);
console.assert(hostname === 'www.youtube.com'); // true