3

I want to get the top domain from strings such as:

domain.com -> domain.com
subdomain.domain.com -> domain.com
subdomain.domain.co.uk -> domain.co.uk

Have the following function so far:

function gdn(h) {
    return h.substring(h.lastIndexOf(".", h.lastIndexOf(".") - 1) + 1);
}

Problem is that this does not work with co.uk domains. The return for:

hello.domain.co.uk -> co.uk

Want it to be domain.co.uk

Any ideas?

Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • can you check the last index of "domain"..??? – Vinay S Jain Mar 31 '15 at 12:29
  • Unless you know the domain, subdomain or target `co.uk` specifically etc, there's no way to know the difference between `subdomain.domain.com` and `domain.co.uk`, they are technically the same when it comes to number and placement of periods etc. – adeneo Mar 31 '15 at 12:36
  • possible duplicate of [How to get top level domain (base domain) from the url in javascript](http://stackoverflow.com/questions/6449340/how-to-get-top-level-domain-base-domain-from-the-url-in-javascript) – Abhishek Mar 31 '15 at 12:40

4 Answers4

1

I don't think it's possible without checking for specific extensions like ".co.uk". At least it is not possible using just character-counting or by counting dots.

Also confirmed by this answer: How to get domain name only using javascript?

Regarding the answer mentioned above, note that the list of TLD's has grown alot since then, so make sure you update your list, if you want to use that answer as a base for your end solution. Fresh list: List of TLD's from IANA.org

Community
  • 1
  • 1
Gertsen
  • 1,078
  • 20
  • 36
1

Here this should do it for you: http://jsbin.com/rehaqifoca/1/edit?js,console

var re = /([^.]+\.)?([^\.]+\..+)/; 
var str = 'subdomain.domain.co.uk';

var m = str.match(re);

if (m.length > 2)
  console.log(m[2]);
NYTom
  • 524
  • 2
  • 14
0

Try this it will work to get top domain (Fiddle Link):

HTML Code:

<input type="text" id="url">
<br/>
<input type="button" value="Get Top Domain" id="getTopDomain"/>

Script to get top domain:

jQuery('#getTopDomain').click(function() {
    var regex = new RegExp(/\./g)
    var text = jQuery('#url').val();
    var count = text.match(regex).length;

    if(count > 1) {
        text = text.substring(text.indexOf('.')+1);
    }
    alert(text);
});

Thanks,

~Chandan

-2
function parseDomain(url) {
  var elts = url.split('.');
  var offset = 2;
  if(elts[elts.length-2] == 'co') {
    offset = 3
  }
  return elts.slice(elts.length-offset, elts.length).join('.')
}

var url = "subdomain.domain.co.uk"
console.log(parseDomain(url))
var url2 = "subdomain.domain.com"
console.log(parseDomain(url2))
jmgross
  • 2,306
  • 1
  • 20
  • 24
  • 1
    So now you have problems with `.org.uk` domains. It's just not going to work. – Scimonster Mar 31 '15 at 12:42
  • I agree now, it's impossible without creating a second-level domain exception list – jmgross Mar 31 '15 at 12:46
  • You also have the problem in the other direction, if a person registers `co.[TLD]` on a TLD that doesn't have a national `co` second-level domain (for example, `foo.co.com` should have `co.com` as its primary domain, but your function currently gives `foo.co.com`). As you've said, it's a theoretical impossibility unless you embed national domain policies into your function somehow. – apsillers Mar 31 '15 at 12:49