url.hostname
获取和设置网址的主机名部分。
url.host
和 url.hostname
之间的主要区别在于 url.hostname
不包括端口。
const myURL = new URL('https://example.org:81/foo');
console.log(myURL.hostname);
// 打印 example.org
myURL.hostname = 'example.com:82';
console.log(myURL.href);
// 打印 https://example.com:81/foo
分配给 hostname
属性的无效主机名值将被忽略。
Gets and sets the host name portion of the URL. The key difference between
url.host
and url.hostname
is that url.hostname
does not include the
port.
const myURL = new URL('https://example.org:81/foo');
console.log(myURL.hostname);
// Prints example.org
myURL.hostname = 'example.com:82';
console.log(myURL.href);
// Prints https://example.com:81/foo
Invalid host name values assigned to the hostname
property are ignored.