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 接口。