url.port


获取及设置 URL 的端口部分。

端口值可以是数字或包含 065535(含)范围内的数字字符串。 将值设置为给定 protocolURL 对象的默认端口将会导致 port 值变为空字符串('')。

端口值可以是空字符串,在这种情况下,端口取决于协议/规范:

协议端口
"ftp"21
"file"
"gopher"70
"http"80
"https"443
"ws"80
"wss"443

在为端口分配值后,将首先使用 .toString() 将值转换为字符串。

如果该字符串无效但以数字开头,则将前导号码分配给 port。 如果数字位于上述范围之外,则忽略它。

const myURL = new URL('https://example.org:8888');
console.log(myURL.port);
// 打印 8888

// 默认端口将自动转换为空字符。
// (HTTPS 协议默认端口是 443)
myURL.port = '443';
console.log(myURL.port);
// 打印空字符
console.log(myURL.href);
// 打印 https://example.org/

myURL.port = 1234;
console.log(myURL.port);
// 打印 1234
console.log(myURL.href);
// 输出 https://example.org:1234/

// 完全无效的端口字符串将被忽略。
myURL.port = 'abcd';
console.log(myURL.port);
// 打印 1234

// 开头的数字将会被当做端口号。
myURL.port = '5678abcd';
console.log(myURL.port);
// 打印 5678

// 非整形数字将会被截断。
myURL.port = 1234.5678;
console.log(myURL.port);
// 打印 1234

// 超出范围的数字将被忽略。
myURL.port = 1e10; // 10000000000,将按如下所述进行范围检查。
console.log(myURL.port);
// 打印 1234

包含小数点的数字(例如浮点数或科学计数法中的数字)不是此规则的例外。 假设它们有效,将小数点前的前导数字设置为 URL 的端口:

myURL.port = 4.567e21;
console.log(myURL.port);
// 打印 4 (因为它是字符串 '4.567e21' 中的前导数字)

Gets and sets the port portion of the URL.

The port value may be a number or a string containing a number in the range 0 to 65535 (inclusive). Setting the value to the default port of the URL objects given protocol will result in the port value becoming the empty string ('').

The port value can be an empty string in which case the port depends on the protocol/scheme:

protocolport
"ftp"21
"file"
"gopher"70
"http"80
"https"443
"ws"80
"wss"443

Upon assigning a value to the port, the value will first be converted to a string using .toString().

If that string is invalid but it begins with a number, the leading number is assigned to port. If the number lies outside the range denoted above, it is ignored.

const myURL = new URL('https://example.org:8888');
console.log(myURL.port);
// Prints 8888

// Default ports are automatically transformed to the empty string
// (HTTPS protocol's default port is 443)
myURL.port = '443';
console.log(myURL.port);
// Prints the empty string
console.log(myURL.href);
// Prints https://example.org/

myURL.port = 1234;
console.log(myURL.port);
// Prints 1234
console.log(myURL.href);
// Prints https://example.org:1234/

// Completely invalid port strings are ignored
myURL.port = 'abcd';
console.log(myURL.port);
// Prints 1234

// Leading numbers are treated as a port number
myURL.port = '5678abcd';
console.log(myURL.port);
// Prints 5678

// Non-integers are truncated
myURL.port = 1234.5678;
console.log(myURL.port);
// Prints 1234

// Out-of-range numbers which are not represented in scientific notation
// will be ignored.
myURL.port = 1e10; // 10000000000, will be range-checked as described below
console.log(myURL.port);
// Prints 1234

Numbers which contain a decimal point, such as floating-point numbers or numbers in scientific notation, are not an exception to this rule. Leading numbers up to the decimal point will be set as the URL's port, assuming they are valid:

myURL.port = 4.567e21;
console.log(myURL.port);
// Prints 4 (because it is the leading number in the string '4.567e21')