此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Location:host 属性

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

Location 接口的 host 属性是一个字符串,包含主机名(即 hostname),如果 URL 的端口非空,则会在主机名后追加一个 ":"port。如果 URL 没有 hostname,则该属性的值为空字符串 ""

更多详细信息请参阅 URL.host

字符串。

示例

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

anchor.href = "https://www.supremezsy.dpdns.org/zh-CN/Location.host";
console.log(anchor.host === "www.supremezsy.dpdns.org");

anchor.href = "https://www.supremezsy.dpdns.org:443/zh-CN/Location.host";
console.log(anchor.host === "www.supremezsy.dpdns.org");
// 由于 443 是该协议方案的默认端口号,因此未包含此端口号。

anchor.href = "https://www.supremezsy.dpdns.org:4097/zh-CN/Location.host";
console.log(anchor.host === "www.supremezsy.dpdns.org:4097");

规范

规范
HTML
# dom-location-host-dev

浏览器兼容性