URL:port 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

备注: 此特性在 Web Worker 中可用。

URL 接口的 port 属性是一个表示 URL 端口号的字符串。

备注: 如果传递给 URL() 构造函数的输入字符串不包含显式端口号(例如 https://localhost),或者包含的端口号是与输入字符串的协议部分对应的默认端口号(例如 https://localhost:443),那么在构造函数返回的 URL 对象中,端口属性的值将为空字符串:''

字符串。

示例

js
// 使用非默认端口号的 https 协议
new URL("https://example.com:5443/svn/Repos/").port; // '5443'
// 使用非默认端口号的 http 协议
new URL("http://example.com:8080/svn/Repos/").port; // '8080'
// 使用默认端口号的 https 协议
new URL("https://example.com:443/svn/Repos/").port; // ''(空字符串)
// 使用默认端口号的 http 协议
new URL("http://example.com:80/svn/Repos/").port; // ''(空字符串)
// 没有明确端口号的 https 协议
new URL("https://example.com/svn/Repos/").port; // ''(空字符串)
// 没有明确端口号的 http 协议
new URL("https://example.com/svn/Repos/").port; // ''(空字符串)
// 使用非默认端口号的 ftp 协议
new URL("ftp://example.com:221/svn/Repos/").port; // '221'
// 使用默认端口号的 ftp 协议
new URL("ftp://example.com:21/svn/Repos/").port; // ''(空字符串)

规范

Specification
URL
# dom-url-port

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
port

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见

  • 所属的 URL 接口。