Home > OS >  How to get subdomain from url
How to get subdomain from url

Time:10-21

I have a url like http://abc.xyz.com. I just want 'http://abc' part, But I am getting 'http://abc.xyz.com'

I tried this:

windw.location.origin

Do I need to write some extra method to get the first part of url or is there something any window method which will do the same?

CodePudding user response:

You can split it by . and take the first element

window.location.origin.split('.')[0]

Example:

console.log(window.location.origin);
console.log(window.location.origin.split('.')[0]);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

"http://abc.xyz.com".split(".")[0]
// or
var url = "http://abc.xyz.com";
url.split(".")[0]

returns "http://abc"

CodePudding user response:

It's as simple as that: [subdomain] = 'https://abc.def.xyz'.split('.');

var url = 'https://abc.def.xyz';
[subdomain] = url.split('.');
console.log(subdomain);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related