简体   繁体   English

如何从url获取子域

[英]How to get subdomain from url

I have a url like http://abc.xyz.com .我有一个像http://abc.xyz.com这样的网址。 I just want 'http://abc' part, But I am getting 'http://abc.xyz.com'我只想要'http://abc'部分,但我得到'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?我是否需要编写一些额外的方法来获取 url 的第一部分,或者是否有任何window方法可以做同样的事情?

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]);

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

returns "http://abc"返回“http://abc”

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

 var url = 'https://abc.def.xyz'; [subdomain] = url.split('.'); console.log(subdomain);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM