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

342
Views
Simplest way to check if the current url contains a subdomain

I'm looking for the simplest way to check if the user is on the normal domain (domain.com) or is on a subdomain et.domain.com and display content based on that. If it matters I'm trying to do that on shopify.

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

0

You can split the url with dot(.) and check the length. This will only work for .com url.

Note: This will not work for domains like google.co.in

const domain = 'domain.com';
const subDomain = 'et.domain.com'

const isSubdomain = (domain) => domain.split('.').length > 2;

console.log(isSubdomain(domain));
console.log(isSubdomain(subDomain));

about 4 years ago · Juan Pablo Isaza Report

0

You can actually use regex method.

var isSubdomain = function(url) {
    url = url || 'http://www.test-domain.com'; // just for the example
    var regex = new RegExp(/^([a-z]+\:\/{2})?([\w-]+\.[\w-]+\.\w+)$/);

    return !!url.match(regex); // make sure it returns boolean
}

console.log(isSubdomain("example.com"));
console.log(isSubdomain("http://example.com:4000"));
console.log(isSubdomain("www.example.com:4000"));
console.log(isSubdomain("https://www.example.com"));
console.log(isSubdomain("sub.example.com"));
console.log(isSubdomain("example.co.uk")); //it doesn't work on these very specific cases

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!