What is the expected behavior of a URL preceded with “//” when not linked from an existing page?

戏子无情 提交于 2019-12-11 08:08:32

问题


A nice trick to avoid insecure content warnings on pages that could be either http or https is to reference all scripts or assets in the code using "//" which will use the current page protocol instead of a specified one.

If one enters a URL with // in the front directly into a browser URL box, is there a default behavior the browser will pick? Would/should it default to http or https, or some set of rules to test and pick one over the other?


回答1:


A network-path reference (e.g., //example.com/) is a relative reference. For resolving a relative reference, a base URI is necessary.

When entering a network-path reference into a browser’s address bar, no such base URI can be established with the first three ways, so the fourth way, 5.1.4. Default Base URI, applies:

If none of the conditions described above apply, then the base URI is defined by the context of the application. As this definition is necessarily application-dependent, failing to define a base URI by using one of the other methods may result in the same content being interpreted differently by different types of applications.

In other words, it’s up to each browser.

If a browser would only support http and https, it would likely choose the same scheme that gets used when users enter something like "www.example.com", so probably http (see Suffix Reference). But many browsers support more schemes.

For example, on my system, requesting //example.com/test resolves to a URI using the file scheme: file:////example.com/test (Firefox), file:///example.com/test (Chromium).



来源:https://stackoverflow.com/questions/46738747/what-is-the-expected-behavior-of-a-url-preceded-with-when-not-linked-from-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!