Location: host 属性

hostLocation 接口的一个字符串属性,包含主机,即主机名,以及如果 URL 的端口非空,则包含一个 ':' 和 URL 的端口

字符串。

示例

js
const anchor = document.createElement("a");

anchor.href = "https://mdn.org.cn/en-US/Location.host";
console.log(anchor.host === "developer.mozilla.org");

anchor.href = "https://mdn.org.cn:443/en-US/Location.host";
console.log(anchor.host === "developer.mozilla.org");
// The port number is not included because 443 is the scheme's default port

anchor.href = "https://mdn.org.cn:4097/en-US/Location.host";
console.log(anchor.host === "developer.mozilla.org:4097");

规范

规范
HTML 标准
# dom-location-host-dev

浏览器兼容性

BCD 表格仅在启用了 JavaScript 的浏览器中加载。