0

How can I get the domain without subdomain in JS without jQuery?

I saw a lot of solutions with split and regular expressions that doesn't work in all this cases:

  • http:// subdomain.domain.com.br
  • http:// subdomain.domaincom
  • http:// www.domain.com
  • http:// www.domain.com.br

The result may be: domain.com / domain.com.br

JJJ
  • 32,902
  • 20
  • 89
  • 102
Alexandra
  • 155
  • 2
  • 4
  • 15
  • You can't do that with just a regex without a [list of all TLD](http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains). – Denys Séguret Oct 31 '14 at 09:35
  • The perfect solution without regx ....................................... var hostname = "your url"; var parts = location.hostname.split('.'); var subdomain = parts.shift(); var upperleveldomain = parts.join('.'); To get only the second-level-domain, you might use var sndleveldomain = parts.slice(-2).join('.'); – Shailendra Sharma Oct 31 '14 at 09:41
  • @ShailendraSharma i already see and test this "solution" but It doesn't work for **all** cases mentioned before – Alexandra Oct 31 '14 at 13:11

0 Answers0