31
votes

How i can get the domain name example.com from the set of possible subdomains sub1.example.com sub2.example.com sub3.example.com using javascript ...?

6
What about sub1.example.co.uk? publicsuffix.orgSLaks
This is one of the options, thanks for the correctionAleksov
Do you want the domain name for sub3.example.com.ua to be example.com or example.com.ua?Matt Zeunert
example.com.ua I need to allow the use of a script on a specific domain and its subdomains, while prohibiting its use in other domains. So I need to make a selection for the domain name.Aleksov

6 Answers

84
votes
var parts = location.hostname.split('.');
var subdomain = parts.shift();
var upperleveldomain = parts.join('.');

To get only the second-level-domain, you might use

var parts = location.hostname.split('.');
var sndleveldomain = parts.slice(-2).join('.');
4
votes

This is faster

const firstDotIndex = subDomain.indexOf('.');
const domain = subDomain.substring(firstDotIndex + 1);
4
votes

The accepted answer will work to get the second level domain. However, there is something called "public suffixes" that you may want to take into account. Otherwise, you may get unexpected and erroneous results.

For example, take the domain www.amazon.co.uk. If you just try getting the second level domain, you'll end up with co.uk, which is probably not what you want. That's because co.uk is a "public suffix", which means it's essentially a top level domain. Here's the definition of a public suffix, taken from https://publicsuffix.org:

A "public suffix" is one under which Internet users can (or historically could) directly register names.

If this is a crucial part of your application, I would look into something like psl (https://github.com/lupomontero/psl) for domain parsing. It works in nodejs and the browser, and it's tested on Mozilla's maintained public suffix list.

Here's a quick example from their README:

var psl = require('psl');

// TLD with some 2-level rules.
psl.get('uk.com'); // null);
psl.get('example.uk.com'); // 'example.uk.com');
psl.get('b.example.uk.com'); // 'example.uk.com');
1
votes

The generic solution is explained here http://rossscrivener.co.uk/blog/javascript-get-domain-exclude-subdomain From above link

var domain = (function(){
   var i=0,domain=document.domain,p=domain.split('.'),s='_gd'+(new Date()).getTime();
   while(i<(p.length-1) && document.cookie.indexOf(s+'='+s)==-1){
      domain = p.slice(-1-(++i)).join('.');
      document.cookie = s+"="+s+";domain="+domain+";";
   }
   document.cookie = s+"=;expires=Thu, 01 Jan 1970 00:00:01 GMT;domain="+domain+";";
   return domain;
})();
0
votes

    function getDomain() {
        const hostnameArray = window.location.hostname.split('.')
        const numberOfSubdomains = hostnameArray.length - 2
        return hostnameArray.length === 2 ? window.location.hostname : hostnameArray.slice(numberOfSubdomains).join('.')
    }
    console.log(getDomain());

This will remove all subdomains, so "a.b.c.d.test.com" will become "test.com"

0
votes

If you want to verify if a specific subdomain exists

var parts = location.hostname.split('.');
if(parts.includes('subdomain_to_search_here')){
   //yes
}else{
   //no
}