Get root domain from location.host
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.
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:
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'