0

How do I get the domain name from a subdomain?

Like:

URL -> Output
m.gigabyte.com -> gigabyte.com
forums.localhost -> localhost

I have tried:

var domain = window.location.host.split( '.' );
window.location = 'http://' + domain[1] + domain[2] + window.location.pathname;

But it doesn't seem to work on localhost. I just want to strip off the subdomain and not the .com, .org, .net's anymore...

KevinT.
  • 272
  • 6
  • 16
  • Here is the answer http://stackoverflow.com/questions/13367376/get-the-domain-name-of-the-subdomain-javascript also read http://www.interactivebynature.com/2011.04.09.remove-subdomain-from-url-with-javascript – Aamir Afridi May 19 '14 at 11:26
  • What about domains like `.co.uk`? Sometimes it can be very hard to know where primary domain stops and subdomain starts. – spender May 19 '14 at 11:26

1 Answers1

0

How about this:

var subdomain = 'aaaa.bbb.ccc.com';
var dotIndex = subdomain.indexOf('.');
var maindomain = subdomain.substr(dotIndex+1);
Guilhem Soulas
  • 1,975
  • 2
  • 18
  • 30