URL:port 屬性

Baseline 已廣泛支援

此特性已非常成熟,可在多種裝置和瀏覽器版本上使用。自 ⁨2016 年 9 月⁩以來,它已在各大瀏覽器中可用。

注意:此功能在 Web Workers 中可用。

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

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

字串。

示例

js
// https protocol with non-default port number
new URL("https://example.com:5443/svn/Repos/").port; // '5443'
// http protocol with non-default port number
new URL("http://example.com:8080/svn/Repos/").port; // '8080'
// https protocol with default port number
new URL("https://example.com:443/svn/Repos/").port; // '' (empty string)
// http protocol with default port number
new URL("http://example.com:80/svn/Repos/").port; // '' (empty string)
// https protocol with no explicit port number
new URL("https://example.com/svn/Repos/").port; // '' (empty string)
// http protocol with no explicit port number
new URL("http://example.com/svn/Repos/").port; // '' (empty string)
// ftp protocol with non-default port number
new URL("ftp://example.com:221/svn/Repos/").port; // '221'
// ftp protocol with default port number
new URL("ftp://example.com:21/svn/Repos/").port; // '' (empty string)

規範

規範
URL
# dom-url-port

瀏覽器相容性

另見

  • 它所屬的 URL 介面。