位置:hostname 屬性
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 |
瀏覽器相容性
載入中…