位置:hostname 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

Location 介面的 hostname 屬性是一個字串,其中包含 location URL 的域名IP 地址。如果 URL 沒有主機名,則此屬性包含一個空字串,""。IPv4 和 IPv6 地址會被規範化,例如去除前導零,域名會被轉換為 IDN(國際化域名)。

有關更多資訊,請參閱 URL.hostname

字串。

示例

js
console.log(window.location.hostname);
// developer.mozilla.org

const anchor = document.createElement("a");
anchor.href = "https://mdn.club.tw:4097/";
console.log(anchor.hostname === "developer.mozilla.org");
// The port number is not included in hostname

規範

規範
HTML
# dom-location-hostname-dev

瀏覽器相容性