--- title: URL.host slug: Web/API/URL/host translation_of: Web/API/URL/host ---
{{ApiRef("URL API")}}

{{domxref("URL")}} 接口的 host 属性是一个 {{domxref("USVString")}} 值,包含了主机信息,也就是 主机名(hostname),还有,如果 URL 接口不为空,也会包含冒号 ':' 和 URL 的 端口(port)

{{AvailableInWorkers}}

语法

string = object.host;
object.host = string;

 

返回值

{{domxref("USVString")}}.

 

示例

var url = new URL('https://developer.mozilla.org/en-US/docs/Web/API/URL/host');
var result = url.host // "developer.mozilla.org"

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

var url = new URL('https://developer.mozilla.org:4097/en-US/docs/Web/API/URL/host');
var result = url.host // "developer.mozilla.org:4097"

详情

Specification Status Comment
{{SpecName('URL', '#dom-url-host', 'URL.host')}} {{Spec2('URL')}} 初始定义

浏览器兼容

{{Compat("api.URL.host")}}

参考