Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the domain name of the subdomain Javascript

How i can get the domain name example.com from the set of possible subdomains sub1.example.com sub2.example.com sub3.example.com using javascript ...?

like image 235
Aleksov Avatar asked Nov 13 '12 19:11

Aleksov


People also ask

How do I find the domain of a string?

Firstly, we'd need to extract the host from the given URL value. We can use the URI class: String urlString = "https://www.baeldung.com/java-tutorial"; URI uri = new URI(urlString); String host = uri. getHost();


1 Answers

var parts = location.hostname.split('.'); var subdomain = parts.shift(); var upperleveldomain = parts.join('.'); 

To get only the second-level-domain, you might use

var parts = location.hostname.split('.'); var sndleveldomain = parts.slice(-2).join('.'); 
like image 106
Bergi Avatar answered Sep 20 '22 11:09

Bergi