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

标签 javascript url https port

我想读取我拥有的 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);

最佳答案

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/

相关文章:

javascript - 如何使用 angularjs 设计具有阶段和可扩展输入的动态进度条?

javascript - react js。如何制作具有这种结构的组件: <Component> description of the component </Component>

php - 通过 PHP 从 URL 获取 .tld

javascript - 如何修复 nginx 上的 http 414 Request-URI Too Large 错误?

Java Web 启动 : Unable to tunnel through proxy since Java 8 Update 111

javascript - document.ready 未触发 - 除非其中包含 'alerts'

javascript - 是否可以将对象原型(prototype)上的方法组织到 namespace 中?

git - 为什么 Github 项目文档页面 url 区分大小写?有什么负面影响?

asp.net-mvc - 如何通过URL获取RouteData?

javascript - MixedContent 当我通过 ajax 加载 https 页面时,但浏览器仍然认为它是 http