25

If I have a hostname such as: http://sample.example.com and in Javascript I do window.location.hostname, would I get "example.com" or "sample.example.com"?

If not, how would I be able to get sample.example.com?

LB.
  • 13,102
  • 23
  • 65
  • 102

10 Answers10

26

Yes, window.location.hostname will give you subdomains as well. If this isn't working, or isn't supported by some other browser, you could quite easily parse for it:

// window.location.href == "http://sample.somedomain.com/somedir/somepage.html"
var domain = /:\/\/([^\/]+)/.exec(window.location.href)[1];
nickf
  • 520,029
  • 197
  • 633
  • 717
  • nickf, thanks! What is /:\/\/([^\/]+)/ ? Regex expression to execute against? – LB. Jul 29 '09 at 16:09
  • yes: it finds everything from the first :// to the end or the next / ... this will be the subdomains, domain and tld stuff – nickf Jul 29 '09 at 16:35
13

This does the trick for me:

var host = window.location.host
var subdomain = host.split('.')[0]
jaredbaszler
  • 3,151
  • 2
  • 32
  • 38
Rudin Swagerman
  • 131
  • 1
  • 2
12

It can be done as below:

var subdomain =  window.location.host.split('.')[1] ? window.location.host.split('.')[0] : false;
Mittal Patel
  • 2,659
  • 12
  • 23
  • 2
    Please consider explaining your answer. – Avag Sargsyan Jan 09 '18 at 10:39
  • it is the seme of x = a ? b : c ; => if 'a' exist 'x' will be = 'b' otherwise 'x' will equal 'c',, so id you call sub.mydoman.com , (the second valu in the split with exist so it mean you have subsoman whiche is '' sub ' ) but you you call so mydoman.com , the second part of split of location host will not exist , so it gave null . so you will have subdoman = 'false ' , by this way you can get subdomain , or false ; – Ilhami Mounir Aziz Girgis Jan 18 '18 at 11:11
6

I know this is an old question but a more robust answer would be to capture all subdomains. It's possible to have nested subdomains such as https://my.company.website.com. In order to adequately capture all subdomains, I think this is the simplest answer:

// for https://my.company.website.com,
const subdomain = window.location.hostname.split('.').slice(0, -2).join('.');
console.log(subdomain); // "my.company"
tennisgent
  • 13,995
  • 9
  • 48
  • 47
5

First of all, it's window.location, not document.location (document.location works in some browsers but it is not standard)

And yes, location.hostname will return the entire domain name, including any subdomains

Read more here

Window Location

Alan Wells
  • 29,226
  • 15
  • 91
  • 141
Peter Bailey
  • 103,526
  • 30
  • 178
  • 200
3

Yes alert(window.location.hostname) will include subdomains like 'www' and 'sample'.

Al.
  • 2,872
  • 2
  • 20
  • 34
3
const subdomain = window.location.hostname.split(".")[0]

window.location.hostname return string include subdomain - main domain - ltd
so you can easily get the first word by converting it to an array then getting first item

2

How about this snippet. It might help:

var a = new String(window.location);
a = a.replace('http://','');
a = a.substring(0, a.indexOf('/'));
alert(a);
Marcin
  • 7,714
  • 6
  • 43
  • 49
2

with array destructuring you can do this:

// window.location.host = "meta.stackoverflow.com"
const [ , , subdomain] = window.location.hostname.split(".").reverse();
// console.log(subdomain);
// "meta"
yue
  • 141
  • 1
  • 4
0

I recommend using the npm package psl (Public Suffix List). You can look this link: npm psl