url.hostname


获取和设置 URL 的主机名部分。url.hosturl.hostname 的关键区别在于,url.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

// Setting the hostname does not change the port
myURL.hostname = 'example.com';
console.log(myURL.href);
// Prints https://example.com:81/foo

// Use myURL.host to change the hostname and port
myURL.host = 'example.org:82';
console.log(myURL.href);
// Prints https://example.org:82/foo 

分配给 hostname 属性的无效主机名值会被忽略。

【Invalid host name values assigned to the hostname property are ignored.】