Get root domain from location.host
Asked Answered
A

5

12

There are a lot of SO questions that seem to address variations of this question. But they tend to be complex regex answers and I am hoping I can find something simpler.

Given location.host values of

foo.mysite.com
app.foo.mysite.com
mysite.com

How can I get the root domain mysite.com?

I could do something like finding the second to last ., but this seems ugly and wouldn't work for any TLD's like .co.uk. If jQuery has an object that contains this information I am happy to use it.

My goal is to create cookies that exist across all subdomains. To do this I need to find .mysite.com. I'd prefer not to hardcode it.

Alexandros answered 2/5, 2012 at 21:47 Comment(6)
Given a list of valid TLDs it shouldn't be hard - instead of the second to last . you want the first . (reading right-to-left) after the TLD. You could do this with a big regexp or a loop, but the latter would typically be less efficient.Depressant
A complete list can be found here: publicsuffix.org Whether you use a regex or some other way won't make much of a difference I guess. In any case you have to consider all the public TLDs and SLDs. Have a look at #4453416.Aminta
There's a project to do this at github.com/riffraff/publicsuffix.js but it claims to be "broken." I haven't tried it but it could form the basis for your solution.Kiser
@FelixKling - thanks, that's perfect. Voting to close my own question.Alexandros
If it really helped you, you can also just delete your question... just saying :) Happy coding!Aminta
There are countries with optional 2nd level domains (i.e. both some-domain.xy and some-domain.co.xy are valid). I don't think you can make (reasonably small) generic solution. Just put the constant in some configuration file and print it where needed.Gaziantep
A
11

Given the extremely low likelihood that our domain would ever change from anything other than .com, let alone to a SLD, I coded something like this in.

var temp = location.host.split('.').reverse();
var root_domain = '.' + temp[1] + '.' + temp[0];

The overhead and maintenance of maintaining a TLD or SLD list and comparing against it is not worth the trade off for us.

Alexandros answered 3/5, 2012 at 20:30 Comment(2)
tldjs library can be used if you want to avoid the maintenance yourself. It is based on Mozilla public suffix listLauncher
You may want to use location.hostname rather than host - see #6726390Flyspeck
A
16

if you want it all on one line -

document.domain.split('.').reverse().splice(0,2).reverse().join('.')

or

location.hostname.split('.').reverse().splice(0,2).reverse().join('.')

for inputs: 'foo.example.com', 'foo.bar.example.com', 'foo.bar.fizz.buzz.example.com'

it will return: 'example.com'

Arceliaarceneaux answered 15/6, 2017 at 17:4 Comment(2)
Doesn’t work for .co.uk domains.Prude
it can be simplified: location.hostname.split('.').slice(-2).join('.')Ineffectual
A
11

Given the extremely low likelihood that our domain would ever change from anything other than .com, let alone to a SLD, I coded something like this in.

var temp = location.host.split('.').reverse();
var root_domain = '.' + temp[1] + '.' + temp[0];

The overhead and maintenance of maintaining a TLD or SLD list and comparing against it is not worth the trade off for us.

Alexandros answered 3/5, 2012 at 20:30 Comment(2)
tldjs library can be used if you want to avoid the maintenance yourself. It is based on Mozilla public suffix listLauncher
You may want to use location.hostname rather than host - see #6726390Flyspeck
H
6

You cannot call .co.uk as TLD. It is actually a second level domain. So it'll always be ambiguous that what is the root domain.
However you can list all the available TLD's and Second Level Domains to and try to find a match. But that will be a very costly and tedious operation.
If you want to do this, this List of TLDs and SLDs might be useful:

Hyunhz answered 2/5, 2012 at 22:22 Comment(0)
R
3

I've been using this:

const rootDomain = s => {
    const r =  /.*\.([^.]*[^0-9][^.]*\.[^.]*[^.0-9][^.]*$)/;
    return s.replace(r, '$1');
};

Which is similar in output to the above method using split, reverse etc., but it doesn't modify straight IP addresses.

Rea answered 19/2, 2020 at 10:29 Comment(0)
D
0
const rootDomain = '.' + window.location.hostname.split('.').slice(-2).join('.');
Dated answered 23/4, 2020 at 7:28 Comment(3)
Although this code might solve the problem, a good answer should also explain what the code does and how it helps.Dewan
@Dewan This code is pretty standard and self-explaining. What more do you expect?Ill
I suggest trimming this down to const rootDomain = location.hostname.split('.').slice(-2).join('.');Keystroke

© 2022 - 2024 — McMap. All rights reserved.