URL:host 属性

Baseline 已广泛支持

此特性已稳定,并兼容多种设备和浏览器版本。自 ⁨2015 年 11 月⁩起,所有浏览器均已支持此特性。

注意:此功能在 Web Workers 中可用。

host 属性是 URL 接口的一个字符串,包含主机名 (hostname),如果 URL 的 端口 (port) 不为空,则紧跟一个 ":",然后是 URL 的 port。如果 URL 没有 hostname,则此属性包含一个空字符串,""

可以设置此属性来更改 URL 的主机名和端口。如果 URL 的 scheme 不是分层的(URL 标准称之为“特殊 scheme”),则它没有主机的概念,设置此属性无效。

注意: 如果用于 host setter 的给定值缺少 port,则 URL 的 port 不会更改。这可能出乎意料,因为 host getter 返回一个包含 URL 端口的字符串,所以人们可能会认为 setter 总是会“重置”两者。

字符串。

示例

js
let url = new URL("https://mdn.org.cn/en-US/docs/Web/API/URL/host");
console.log(url.host); // "developer.mozilla.org"

url = new URL("https://mdn.org.cn:443/en-US/docs/Web/API/URL/host");
console.log(url.host); // "developer.mozilla.org"
// The port number is not included because 443 is the scheme's default port

url = new URL("https://mdn.org.cn:4097/en-US/docs/Web/API/URL/host");
console.log(url.host); // "developer.mozilla.org:4097"

规范

规范
URL
# dom-url-host

浏览器兼容性

另见

  • 它所属的 URL 接口。