我想读取我拥有的 url
字符串的端口。我发现您可以使用 urlstring
作为参数创建一个新的 URL 对象。然后您可以调用该对象的 port 属性来获取 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);
最佳答案
443
是 https
的默认端口
....
---> 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/