What is the expected behavior of a URL preceded wi

2019-07-27 08:02发布

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条回答
对你真心纯属浪费
2楼-- · 2019-07-27 08:25

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).

查看更多
登录 后发表回答