2

I want to check if the subdomain demo exists in my URL.

So if my URL is either https://demo.stackoverflow.com/ or https://demo.stacks.com/ or https://demo.hello.com/, the function should return true.

If the URL is just https://stackoverflow.com/, without the word demo, the function should return false.

So how can I do that?

Current Code

<script>
  if (window.location.hostname === 'https://demo.stackoverflow.com/') {
    document.write('<script src="./script.min.js"></' + 'script>');
  }
</script>
mfluehr
  • 2,832
  • 2
  • 23
  • 31
Joseph
  • 7,042
  • 23
  • 83
  • 181
  • "*I wanted to check if `demo` exist in my URL.*" What about `https://example.com/demo`, should that match? What about `https://example.com/file.php?q=demo`? – esqew Nov 10 '22 at 15:07
  • You could try the [DNS API](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/dns/resolve), but it isn't well supported. – Michael M. Nov 10 '22 at 15:08
  • 1
    @MichaelM. The DNS API you've linked is only available in Firefox and only in the context of browser extensions. I'm not quite sure that's relevant here. – esqew Nov 10 '22 at 15:10
  • @esqew. demo should be on the subdomain – Joseph Nov 10 '22 at 15:10
  • @Joseph Do you want to check if the subdomain exists, or if the substring in the URL exists? – Michael M. Nov 10 '22 at 15:16

2 Answers2

1

You can use the startsWith method.

window.location.host.startsWith('demo.')

This will only work for checking if that is the subdomain though.

Edit: It shouldn't matter if you use host or hostname if you are just checking for a subdomain.

Tim
  • 112
  • 6
1

The URL API has pretty good support, browser-wise. Use it to parse the subdomain(s) from window.location and check if demo is present at any point in the hostname:

function demoSubdomainIsPresent(url) {
  var domains = new URL(url).hostname.split(".");
  return domains.includes("demo");
}

// Should return true:
console.log(demoSubdomainIsPresent('https://demo.example.com'));
console.log(demoSubdomainIsPresent('https://east.demo.example.com'));

// Should return false:
console.log(demoSubdomainIsPresent(window.location)); // window.location for snippets is 'stacksnippets.net', should return false
console.log(demoSubdomainIsPresent('https://example.com'));
console.log(demoSubdomainIsPresent('https://example.com/demo.php'));
console.log(demoSubdomainIsPresent('https://exmaple.com/page.php?q=demo'));
esqew
  • 42,425
  • 27
  • 92
  • 132
  • This answer returns `true` with `https://example.demo.com/` and the OP specifically wants `demo` to be the subdomain. Not sure how this is accepted. – Syed M. Sannan Nov 10 '22 at 15:36
  • @Stranger If you want to get into semantics, [RFC 1034](https://www.rfc-editor.org/rfc/rfc1034) would disagree with your characterization, as `demo.com` would be considered a subdomain of the larger `com` domain: "*A domain is a subdomain of another domain if it is contained within that domain. This relationship can be tested by seeing if the subdomain's name ends with the containing domain's name. For example, A.B.C.D is a subdomain of B.C.D, C.D, D, and " ".*" If OP is after something outside this spec (can't imagine they would be if they've already accepted), they should specify that. – esqew Nov 10 '22 at 15:44
  • I am not talking about semantics. The OP required in their question that the subdomain specifically should be `demo` and nothing else, as they gave their examples. This one allows `example.demo` to slid in so I suggest the answer by @Luke to be correct. – Syed M. Sannan Nov 10 '22 at 15:46
  • @Stranger Unfortunately @Luke's answer also, in its current form, does not meet the requirement as a subdomain does not necessarily need to appear at the very beginning of a hostname for it to be a valid subdomain; their code fails the test case of `https://region.demo.example.com`, which should indubitably be a valid case. – esqew Nov 10 '22 at 15:48
  • Hmm, agreed..... – Syed M. Sannan Nov 10 '22 at 15:49