位置:port 屬性

Baseline 已廣泛支援

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

Location 介面的 port 屬性是一個字串,其中包含 location URL 的埠號。如果埠是協議的預設埠(ws:http:80wss:https:443ftp:21),則此屬性包含一個空字串,""

可以設定此屬性來更改 URL 的埠。如果 URL 沒有 host 或者其 scheme 是 file:,則設定此屬性無效。它還會默默地忽略無效的埠號。

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

字串。

示例

js
// Assume current page is at https://mdn.club.tw/en-US/docs/Location/port
const result = location.port; // Returns:''
js
// Assume another page is at https://mdn.club.tw:8888/en-US/docs/Location/port
const result = location.port; // Returns:'8888'

規範

規範
HTML
# dom-location-port-dev

瀏覽器相容性