如何使用window.location获取子域名?

发布于 2024-07-30 01:26:05 字数 244 浏览 10 评论 0原文

如果我有一个主机名,例如: http://sample.example.com 并且在 Javascript 中我会 window.location.hostname,我会得到“example.com”还是“sample.example.com”?

如果没有,我怎样才能获得sample.example.com?

If I have a hostname such as: http://sample.example.com and in Javascript I do window.location.hostname, would I get "example.com" or "sample.example.com"?

If not, how would I be able to get sample.example.com?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

画骨成沙 2024-08-06 01:26:05

是的,window.location.hostname 也会为您提供子域名。 如果这不起作用,或者不受其他浏览器支持,您可以很容易地解析它:

// window.location.href == "http://sample.somedomain.com/somedir/somepage.html"
var domain = /:\/\/([^\/]+)/.exec(window.location.href)[1];

Yes, window.location.hostname will give you subdomains as well. If this isn't working, or isn't supported by some other browser, you could quite easily parse for it:

// window.location.href == "http://sample.somedomain.com/somedir/somepage.html"
var domain = /:\/\/([^\/]+)/.exec(window.location.href)[1];
说不完的你爱 2024-08-06 01:26:05

我建议使用 npm 包 psl (公共后缀列表)。 您可以查看此链接:npm psl

I recommend using the npm package psl (Public Suffix List). You can look this link: npm psl

春庭雪 2024-08-06 01:26:05

这是分割域部分的简单方法,例如 subdomain.maindomain.extension

// Print Subdomain
console.log(window.location.host.split('.')[0]);

// Print  Maindomain
console.log(window.location.host.split('.')[1]);

// Print  extension 
console.log(window.location.host.split('.')[2]);

It's a simple way to split domain parts like subdomain.maindomain.extension

// Print Subdomain
console.log(window.location.host.split('.')[0]);

// Print  Maindomain
console.log(window.location.host.split('.')[1]);

// Print  extension 
console.log(window.location.host.split('.')[2]);
晨与橙与城 2024-08-06 01:26:05

子域可以有点,用点分割无效。
你可以检查这个库 https://www.npmjs.com/package/psl

这些是有效的子域。

www.example.com
www.foo.example.com
foo.bar.example.com

如果您有服务器,您可以在本地计算机上检查它。 这在我的应用程序中有效
http://dev.test.foo.localhost:3000

Subdomain could have dots, splitting with dot is not valid.
You can check this library https://www.npmjs.com/package/psl

These are valid subdomains.

www.example.com
www.foo.example.com
foo.bar.example.com

You can check it in your local machine if you have a server. This is working in my app
http://dev.test.foo.localhost:3000

怪我闹别瞎闹 2024-08-06 01:26:05

首先,它是 window.location,而不是 document.locationdocument.location 在某些浏览器中有效,但不是标准的)

是的, location.hostname返回整个域名,包括任何子域

在此处阅读更多信息

窗口位置

First of all, it's window.location, not document.location (document.location works in some browsers but it is not standard)

And yes, location.hostname will return the entire domain name, including any subdomains

Read more here

Window Location

年少掌心 2024-08-06 01:26:05

是的,alert(window.location.hostname) 将包含“www”和“sample”等子域。

Yes alert(window.location.hostname) will include subdomains like 'www' and 'sample'.

焚却相思 2024-08-06 01:26:05

这个片段怎么样。 它可能有帮助:

var a = new String(window.location);
a = a.replace('http://','');
a = a.substring(0, a.indexOf('/'));
alert(a);

How about this snippet. It might help:

var a = new String(window.location);
a = a.replace('http://','');
a = a.substring(0, a.indexOf('/'));
alert(a);
幼儿园老大 2024-08-06 01:26:05

通过数组解构,你可以这样做:

// window.location.host = "meta.stackoverflow.com"
const [ , , subdomain] = window.location.hostname.split(".").reverse();
// console.log(subdomain);
// "meta"

with array destructuring you can do this:

// window.location.host = "meta.stackoverflow.com"
const [ , , subdomain] = window.location.hostname.split(".").reverse();
// console.log(subdomain);
// "meta"
私藏温柔 2024-08-06 01:26:05

可以按如下方式完成:

var subdomain =  window.location.host.split('.')[1] ? window.location.host.split('.')[0] : false;

It can be done as below:

var subdomain =  window.location.host.split('.')[1] ? window.location.host.split('.')[0] : false;
南城追梦 2024-08-06 01:26:05
const subdomain = window.location.hostname.split(".")[0]

window.location.hostname 返回字符串包括子域 - 主域 - ltd
因此您可以通过将其转换为数组然后获取第一项来轻松获取第一个单词

const subdomain = window.location.hostname.split(".")[0]

window.location.hostname return string include subdomain - main domain - ltd
so you can easily get the first word by converting it to an array then getting first item

秋风の叶未落 2024-08-06 01:26:05

这对我有用:

var host = window.location.host
var subdomain = host.split('.')[0]

This does the trick for me:

var host = window.location.host
var subdomain = host.split('.')[0]
や三分注定 2024-08-06 01:26:05

我知道这是一个老问题,但更可靠的答案是捕获所有子域。 可以有嵌套子域,例如 https://my.company.website.com。 为了充分捕获所有子域,我认为这是最简单的答案:

// for https://my.company.website.com,
const subdomain = window.location.hostname.split('.').slice(0, -2).join('.');
console.log(subdomain); // "my.company"

I know this is an old question but a more robust answer would be to capture all subdomains. It's possible to have nested subdomains such as https://my.company.website.com. In order to adequately capture all subdomains, I think this is the simplest answer:

// for https://my.company.website.com,
const subdomain = window.location.hostname.split('.').slice(0, -2).join('.');
console.log(subdomain); // "my.company"
开始看清了 2024-08-06 01:26:05

就我而言,我需要一些更通用的东西,所以我编写了这个函数:

const getSubdomainFromHref = href => {
  let a = href.replace("http://", "").replace("https://", "");
  subdomain = a.substring(0, a.indexOf("."));
  return subdomain || "default";
};

In my case I needed something a little more versaitile so I wrote this function:

const getSubdomainFromHref = href => {
  let a = href.replace("http://", "").replace("https://", "");
  subdomain = a.substring(0, a.indexOf("."));
  return subdomain || "default";
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文