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
How about this?
location.hostname.split('.').reverse()[1]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With