gpt4 book ai didi

javascript - 为什么 javascript 在创建 url-Object 时忽略 url-string 的端口?

转载 作者:行者123 更新时间:2023-11-29 22:46:08 24 4
gpt4 key购买 nike

我想读取我拥有的 url 字符串的端口。我发现您可以使用 urlstring 作为参数创建一个新的 URL 对象。然后您可以调用该对象的端口属性来获取 urlstring 的端口。

我的url端口是443。如果我将带有 443 的字符串作为 URL 对象的端口,则 url-object 的端口属性为 ""。如果我选择其他数字作为端口,它工作正常。

有人知道为什么会这样吗?

这是一个代码片段:

const URL_STRING1 = "https://example.com:443/";

let url1 = new URL(URL_STRING1);
console.log(url1.port);

const URL_STRING2 = "https://example.com:442/";

let url2 = new URL(URL_STRING2);
console.log(url2.port);

const URL_STRING3 = "https://example.com:444/";

let url3 = new URL(URL_STRING3);
console.log(url3.port);

最佳答案

443https

的默认端口

URL reference for full specs

....
---> Set url’s port to null, if port is url’s scheme’s default port, and to port otherwise.
^^^^^^^^^^^^^^^^^^^^^
....

Default scheme reference for full specs

A special scheme is a scheme listed in the first column of the following table. A default port is a special scheme’s optional corresponding port and is listed in the second column on the same row.

scheme port
"ftp" 21
"file"
"http" 80
"https" 443
"ws" 80
"wss" 443

关于javascript - 为什么 javascript 在创建 url-Object 时忽略 url-string 的端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58529257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com