Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get second level domain name from URL

Is there a way to get top level domain name from the url

for e.g., "https://images.google.com/blah" => "google"

I found this:

var domain = new URL(pageUrl).hostname; 

but it gives me "images.google.com" instead of just google.

Unit tests I have are:

https://images.google.com   => google
https://www.google.com/blah => google
https://www.google.co.uk/blah => google
https://www.images.google.com/blah => google
like image 909
sublime Avatar asked Sep 19 '14 21:09

sublime


2 Answers

How about this?

location.hostname.split('.').reverse()[1]

like image 163
kechol Avatar answered Oct 10 '22 19:10

kechol


Here's my naive take on solving the issue.

url.split('.').reverse()[1].split('//').reverse()[0]

Supports subdomains, but won't support public suffix SLDs.

like image 28
Etienne Martin Avatar answered Oct 10 '22 17:10

Etienne Martin