• {string}

    获取及设置 URL 的主机名部分。 url.hosturl.hostname 之间的区别是 url.hostname 不包含端口。

    1. const myURL = new URL('https://example.org:81/foo');
    2. console.log(myURL.hostname);
    3. // 打印 example.org
    4. // 设置 hostname 不会更改端口。
    5. myURL.hostname = 'example.com:82';
    6. console.log(myURL.href);
    7. // 打印 https://example.com:81/foo
    8. // 使用 host 会更改主机名和端口。
    9. myURL.host = 'example.org:82';
    10. console.log(myURL.href);
    11. // 打印 https://example.org:82/foo

    hostname 属性设置无效的值则会被忽略。