31

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,730
  • 24
  • 67
  • 102

12 Answers12

29

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
  • 537,072
  • 198
  • 649
  • 721
  • 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
22

This does the trick for me:

var host = window.location.host
var subdomain = host.split('.')[0]
jaredbaszler
  • 3,941
  • 2
  • 32
  • 40
Rudin Swagerman
  • 221
  • 2
  • 2
15

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
  • 14,165
  • 9
  • 50
  • 48
  • 1
    I like this answer because you don't care if you don't have any subdomains. this works with: `subdomain2.subdomain1.domain.com`, `subdomain.domain.com`, `domain.com`. Due to localhost, if I'm in a local environment, I would change the slice to -1 – Lucas Vazquez Nov 23 '22 at 20:25
  • To clarify @LucasVazquez comment, in the example of `domain.com` this code will return an empty string ` `. –  Jan 27 '23 at 00:04
  • 2
    Doesn't work with top-level domains such as `.co.uk` – borisdiakur Feb 11 '23 at 20:41
12

It can be done as below:

var subdomain =  window.location.host.split('.')[1] ? window.location.host.split('.')[0] : false;
Mittal Patel
  • 2,732
  • 14
  • 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
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

Master Morsy
  • 96
  • 1
  • 5
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
  • 30,746
  • 15
  • 104
  • 152
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
3

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

Al.
  • 2,872
  • 2
  • 22
  • 34
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,874
  • 7
  • 45
  • 49
1

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
  • 169
  • 1
  • 4
1

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

0

It's a simple way to split domain parts like subdomain.maindomain.extension

// Print Subdomain
console.log(window.location.host.split('.')[0]);

// Print  Maindomain
console.log(window.location.host.split('.')[1]);

// Print  extension 
console.log(window.location.host.split('.')[2]);
KATHEESKUMAR
  • 147
  • 1
  • 9
0

Subdomain could have dots, splitting with dot is not valid. You can check this library https://www.npmjs.com/package/psl

These are valid subdomains.

www.example.com
www.foo.example.com
foo.bar.example.com

You can check it in your local machine if you have a server. This is working in my app http://dev.test.foo.localhost:3000

feyzullahyildiz
  • 456
  • 4
  • 11