tags:

views:

40

answers:

2

Is there a way to figure out what the top domain for the hostname of the current page is? The problem I have is that the script could be on .com domain, or in an international domain like .co.uk

So for: jobs.telegraph.co.uk - top domain is:telegraph.co.uk jobs.nytimes.com - top domain is nytimes.com

The problem is that location.hostname , and the document.domain give the entire domain.

One route is to have a list of all TLDs (too much to carry around) and parse based on that. Another route was if 2 characters after last ".", than internationaltion - hence last two are the TLD, but that does not hold true for all international domains.

A: 

Does this do it for you?

<script>
var doms = ["telegraph.co.uk","jobs.nytimes.com"];
function getTLD(str) {
  var parts = str.split('.');
  var slice = (parts[parts.length-2].length==2)?parts.length-3: parts.length-2;
  return parts.slice(slice).join('.')
}
for (var i=0;i<doms.length;i++) {
  alert(getTLD(doms[i]));
}

</script>
mplungjan
This fails on (for example) "www.arnewoodpractice.nhs.uk","learning.oriel.w-sussex.sch.uk" and "g.unicauca.edu.co"
Colin Pickard
Yeah and I just found www dot uk dot com too!!!
mplungjan
Erm nhs.uk is the TLD for arnewoodpractice.nhs.uk no?
mplungjan
nhs.uk is amiguous as both a host and a registrar, but "www.thecps.org.uk" fails too and .org.uk is definately not a host. The only TLD there is .uk - but hostnames can be registered at second, third and fourth levels depending on the preceding level - have a look at http://en.wikipedia.org/wiki/.uk if you really want to understand the structure...
Colin Pickard
actually there are worse examples than .uk - .ca used to require registration at at third or fourth level in some cases, so both tpl.toronto.on.ca and gg.ca are valid hostnames. You would have to determine which ruleset a domain was originally registered under to work it out.
Colin Pickard
+1  A: 

I'm not sure this is possible to do completely. It may not be meaningful in a lot of cases either. In your example, jobs.telegraph.co.uk is clearly part of the The Telegraph, which lives at telegraph.co.uk, but in other cases you have subdomains which have no relationship to the second level hostname, as is commonly found with free web hosting providers.

There are even "pseudo-NICs" such as CentralNIC which mess up the system by registering subdomains beneath domains such as uk.com, in which case there is clearly no relationship. See for example avon.uk.com.

Even if you ignore those, there are whole TLDs where the structure is a mess - .uk is one example. There are valid hostnames at the second level such as nhs.uk and mod.uk, most domains are registed at the third level such as bbc.co.uk, but .sch.uk domains can only be registered at the fourth level (i.e in the address http://learning.oriel.w-sussex.sch.uk/ you'd be looking for oriel.w-sussex.sch.uk and w-sussex.sch.uk cannot be a valid hostname)

I'm not sure if this can be done in javascript, but one possibility would be to do whois lookups at each level (i.e. jobs.telegraph.co.uk, telegraph.co.uk, .co.uk) until you get an error message along the lines of "registrations not available at this level", then accept the level below as the hostname. Unfortunately I think these messages vary by registrar, but at least there are less registrars than possible hostname permutations.

Colin Pickard