Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
get hostname but check if hostname has subdomain

I have to get a hostname without suffix and without subdomain.

exp:

URL: https://my-domain.com
URL2: https://my-domain.store.com

So I need only the my-domain how can I get this ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

checkout this

const url = new URL('http://example.com/path/index.html?param=value');
console.log(url.hostname) // gives example.com

and if it has a subdomain:

function getDomain(hostname) {
   hostname = hostname.split('.');
   hostname.reverse();
   return `${hostname[1]}.${hostname[0]}`;
}

url = new URL('http://test.example.com/path/index.html?param=value');
getDomain(url.hostname) // gives example.com

url = new URL('http://example.com/path/index.html?param=value');
getDomain(url.hostname) // gives example.com

taken from here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!