URL:port 属性
注意:此功能在 Web Workers 中可用。
URL 接口的 **port** 属性是一个字符串,其中包含 URL 的端口号。如果端口是协议的默认值(ws: 和 http: 为 80,wss: 和 https: 为 443,ftp: 为 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接口。